Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Its Documented that :

 

(( Validating a new value in a column is as easy as trapping the ColumnChanging event, checking the new value (which can be found in the ProposedValue property of the object passed in the second argument to the event handler), and throwing an exception if it can�t be accepted. For example, the following code rejects future dates assigned to the BirthDate column:

 

Private Sub DataTable_ColumnChanging(ByVal sender As Ob ject, _

ByVal e As DataColumnChangeEventArgs) Handles DataT able.ColumnChanging

If e.Column.ColumnName = "BirthDate" Then

If CDate(e.ProposedValue) > Date.Now Then

Throw New ArgumentException("Invalid birth date

value")

End If

End If

End Sub

 

If the user attempts to enter an invalid birth date in the DataGrid, the old value is automatically restored when the caret leaves the grid cell. Note that the DataGrid absorbs the exception and no error message is shown to the user. Interestingly, you can check the value and throw the exception even in the ColumnChanged event handler: in this case, the value is rejected only when the caret leaves the row (not the column).)) End Document

 

This works fine for me for data entered manually

 

but , when i entered Data by Code using:

 

DataGrid1.Item(DataGrid1.CurrentCell) = String

 

and using Throw to cancel the entered data

 

validation will pop up error message then Break execution

 

Any idea ???

  • Moderators
Posted (edited)

You shouldn't Throw an exception if all you want to do is cancel.

 

I'm not sure what DataColumnChangeEventArgs contains but there must be something there. (I don't have .NET in front of me)

Edited by Robby
Visit...Bassic Software
Posted (edited)

I use Throw to cancel the unvalidated data

 

and this is my code :

 

Private Sub MyDataTable_ColumnChanging(ByVal sender As Object, _

ByVal e As System.Data.DataColumnChangeEventArgs) _

Handles MyDataTable.ColumnChanging

If e.Column.ColumnName.Equals("SessionType") Then

If e.ProposedValue.ToString.Length > e.Column.MaxLength Then

Throw (New Exception("Too Long String"))

End If

End If

End Sub

 

 

which works fine when data entered to the Datagrid column manually ( through keyboard) ,

 

but , when filling this column value by code using:

 

DataGrid1.Item(DataGrid1.CurrentCell) = String

 

 

system will pop error msg after Throw statement which notify of unhandeled exception then exit program .

 

Do i have some thing wrong with my code ?

 

or this is a bug ?

 

and how come the same code works for manual entires

 

and breaks at code entries

Edited by waly
  • *Experts*
Posted

I'm not super-familiar with validating things in the DataGrid, but I'll throw out a few suggestions anyhoo.

 

Have you tried using ColumnChanging to see if you can get to the value before it goes in?

 

A question: Can you set the DataSet directly, instead of the Grid's Item property? I don't know what your code is doing to manually set a cell, but can you set the column value directly in the DataSet? When using bound controls it's usually easier (and cleaner) to set the DataSet directly and have the contol update.

 

Where is the data coming from that you're setting manually? Can you not format the data before putting it in the grid/dataset?

 

If you can put data directly in the DataSet, you could check the DataSet's maxlength and not even get into the ColumnChanged. It slightly duplicates code, though both places (your manual updating and the column changed event) could use the same validation function.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

well , using ColumnChanging event validates and cancel any rejected data perfectly using Throw Exception for any manually entered data

 

and by manually entered data i mean data entered directly to the Datagrid by clicking on the required cell then then writing in it using keyboard.

 

Now the problem is if the same data is not entered manually but by code ( as under Button_Click event or through ComboBox selection) using the code:

 

DataGrid1.Item(DataGrid1.CurrentCell) = String

 

which refers to the selected cell

 

every thing goes fine

 

but when i cancel the entered value in this case using the throw statement an error msg popup and and execution breaks.

 

 

i know that there are other ways to fill and validate datasets , but i'm using DataGrid

Posted

Ami making my self clear ?

 

how come the same code rejects values entered mamually

 

and crashes with values entered by code ?

 

is this is a bug ?

 

or i am missing some thing here ?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...