Datagrid delete problem
(Tuesday, December 21, 2004)
Found the following interesting discussion in the Newsgroups:
datagrid delete problem by:Anonymous
| Hi, I have a problem using the Datagrid control in Vb.Net. I have a datagrid which I programatically bind to a table from a db. In other words, I create a data table from a data adapter.fill, and then set the data source for the dbgrid to the table. Yada yada.
So, this all works fine. It even does some nice things for me. For example, some columns don't allow nulls. In the datagrid, if I select the text in such a column, delete it, and then click on another row, the app automatcially pops up a dialog box which says: "Column 'mycolumn' does not allow null. Do you want to correct the value?" And you can then say yes or no to fix it.
The problem comes when you delete a row after you deleted the text of a non-nullable column. Specifically, do this: 1 - select and delete the text in a column that does not allow nulls 2 - immediatly then click on the row header, which selects the row 3 - hit the delete key
At this point, your app dies with an unhandled exception "Column 'mycolumn' does not allow nulls". So, even though the app is smart enough to warn you when you click on another row if you have null in a non-nullable column, it isnt' smart enough to warn you when you click on the row header to perform a delete.
So, my question is how would you deal with this or catch this exception. There doesn't seem to be a DataGrid.DeleteRow event to catch, and the DataTable.DeleteRow doesn't get fired. Also, I tried catching the DataGrid.RowHeaderSelected event to test the value of the non-nullable column, but at the point of this event the value returned from referencing that cell is still the old value before you deleted it. So I can't test it for null because it's not null yet.
Anyway, if I could catch the datagrid deleterow event and test this value to see if it was null and then terminate the delete row process, that in a nut shell is what I'm trying to do. Or any cheat way to achieve this.
Thanks for any info anyone may have on this wierd problem.
Tony
| | | Reply: by:Ken Tucker [MVP]
| | | Hi,
The datacolumn has an allowdbnull property http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatacolumnclassallowdbnulltopic.asp
Sample code for a datagrid that pops a confirmation box when you delete a row and raises an event when a row is delete. http://www.onteorasoftware.com/downloads/delete.zip
Ken
| | | Reply: by:Anonymous
| | | Ken,
Thanks. The sample code for the ConfirmDeleteDataGrid is interesting and I think I could use this to make it work.
I knew about the Column's AllowDbNull property, but that doesn't work. As I said in the email, from the da.fill it already set this property to false (just to be sure I set the property explicitly to false however), and that's why when I delete the value and then click on another row, it pops up telling me that it can't be null. However, if you delete the value and then click on the Row Header, it doesn't check it yet, thereby giving the user the chance to hit delete before it forces you to fix it.
Anyway, I figured out a way to do it. I'm catching the ColumnChanging event for the data table. If the change is for the column I want, I test the proposed value and if it's dbnull, then I pop a message, change the proposed value back to the old value, and change focus to that cell. Unlike just relying on the columns AllowDbNull property, which doesn't come into play when moving from the cell to the Row Header, by catching and using the ColumnChanging event I'm able to stop it before the user can click on the Row Header and perform the delete.
Thanks for your suggestions, and I may end up doing somthing more like what you sent in the sample code.
Tony
|
|
0 Comments:
Post a Comment