Newbie question Validating SmartDate entry in DataGridView.

Newbie question Validating SmartDate entry in DataGridView.

Old forum URL: forums.lhotka.net/forums/t/4883.aspx


AlexTTNET posted on Thursday, May 22, 2008

I have DataGridView and one column is binding to SmartDate property, sure that is a string.

If user input is DateTime all validations work, but if user entered string like that is not DateTime (like “aaaa”), then DataGrid throwing formatting exception.

In Set property I use standard routine:

set

            {

                CanWriteProperty(true);

                if (_startDate.Text != value)

                {

                    _startDate.Text = value;

                    PropertyHasChanged();

                }

            }  

 

How do I prevent user to enter not DateTime string in DataGridView?

If I use IsDate before set text property, than user will never know what happened.

If I catch exception on DataGrid_Erorr level, Validation rules will not work.

Thank you.

dtabako replied on Friday, May 23, 2008

You could try exposing the property as a SmartDate instead of a string. I don't know if that will work - I haven't used any date properties with in-place grid editing. But it did work for the issue I had with sorting SmartDates within the grid by clicking the column header. When I exposed the property as a string it sorted as a string (i.e. 1/1/08 < 12/22/05). After exposing the property as a SmartDate, it sorted properly.

sergeyb replied on Friday, May 23, 2008

I used the following code (VB version)

 

Set(ByVal value As String)

            CanWriteProperty("EffectiveDate", True)

            If Not _effectiveDate.Text.Equals(value) Then

                Dim testDate As SmartDate = New SmartDate()

                If SmartDate.TryParse(value, testDate) Then

                    _effectiveDate.Text = value

                End If

                PropertyHasChanged("EffectiveDate")

            End If

        End Set

 

 

 

Sergey Barskiy

Senior Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: dtabako [mailto:cslanet@lhotka.net]
Sent: Friday, May 23, 2008 11:51 AM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] Newbie question Validating SmartDate entry in DataGridView.

 

You could try exposing the property as a SmartDate instead of a string. I don't know if that will work - I haven't used any date properties with in-place grid editing. But it did work for the issue I had with sorting SmartDates within the grid by clicking the column header. When I exposed the property as a string it sorted as a string (i.e. 1/1/08 < 12/22/05). After exposing the property as a SmartDate, it sorted properly.

AlexTTNET replied on Friday, May 23, 2008

Thank you.

SmartDate will race an error before property change, because binding trying to convert not date string to date and throw DataBinding error, sure we can catch that error on front end, but in general, idea was to keep validation encapsulated in BO.

TryParse work for string and that is what I did for today, but in that case Validation rules will be not use, because property not changed and an empty string will be displayed

My guess was we can use BrokenRules collection and display error provider icon, as for all other cases.

TryParse work good enough for numeric, because we can display some default value 0D for example if TryParse fails, but what is default value for date?

Did somebody try to use Nullable<DateTime> instead of SmartDate?

dtabako replied on Thursday, May 29, 2008

What about changing the grid column to a masked text box column (can that be done? - again never tried it, I'm just kind of brainstorming here) and providing a date-format mask?

AlexTTNET replied on Thursday, May 29, 2008

That is good idea and I working on it right now.

But question about MaskedTextBoxColumn seems to be out of scope of CSLA discusion.

I try to use MS Implementation in

 http://msdn2.microsoft.com/en-us/library/ms180996.aspx of MaskedTextBoxColumn, but any time I run code and try to set mask, null refernce exeption trowing.

I hope if anyone has proved vorking implementation of MaskedTextBoxColumn for Data Grid and cpould share, because seems to me almost all Csla users early or later faced that problem.

Copyright (c) Marimer LLC