waly Posted October 21, 2003 Posted October 21, 2003 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 ??? Quote
Moderators Robby Posted October 21, 2003 Moderators Posted October 21, 2003 (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 October 21, 2003 by Robby Quote Visit...Bassic Software
waly Posted October 21, 2003 Author Posted October 21, 2003 (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 October 21, 2003 by waly Quote
Moderators Robby Posted October 21, 2003 Moderators Posted October 21, 2003 I was way off track, I miss-understood the question. Quote Visit...Bassic Software
waly Posted October 21, 2003 Author Posted October 21, 2003 I'm appreciating your help i wish i can get any assistance ! Quote
*Experts* Nerseus Posted October 21, 2003 *Experts* Posted October 21, 2003 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 Quote "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
waly Posted October 22, 2003 Author Posted October 22, 2003 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 Quote
waly Posted October 24, 2003 Author Posted October 24, 2003 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 ? Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.