DataGridView - Why doesn't this work?
-
Hi, I'm trying to update the values in a DataGridView. It works ok, and I can see my rows and columns being updated, but when I scroll down the grid, and hit the bottom, it throws an "object not set to an instance of an object" error - and the space occupied by the grid changes to a red cross. I'm obviously doing something wrong, and have narrowed the problem down to this bit of code:
Dim gridrow As DataRowView Dim intItemFoundOnRow As Integer = bindingDataGridView2.Find("File Name", strSearch) If intItemFoundOnRow <> -1 Then gridrow = bindingDataGridView2.Item(intItemFoundOnRow) gridrow.BeginEdit() gridrow.Item("Columns in Database") = CountColumnsInTable(strTableName) gridrow.EndEdit() End If
It isn't anything to do with the CountColumnsInTable function, because I can replace this line with:gridrow.Item("Columns in Database") = 9999
... and it makes no difference. I threw in .BeginEdit and .EndEdit in an attempt to get it to work, but these lines may as well not be there. -
Hi, I'm trying to update the values in a DataGridView. It works ok, and I can see my rows and columns being updated, but when I scroll down the grid, and hit the bottom, it throws an "object not set to an instance of an object" error - and the space occupied by the grid changes to a red cross. I'm obviously doing something wrong, and have narrowed the problem down to this bit of code:
Dim gridrow As DataRowView Dim intItemFoundOnRow As Integer = bindingDataGridView2.Find("File Name", strSearch) If intItemFoundOnRow <> -1 Then gridrow = bindingDataGridView2.Item(intItemFoundOnRow) gridrow.BeginEdit() gridrow.Item("Columns in Database") = CountColumnsInTable(strTableName) gridrow.EndEdit() End If
It isn't anything to do with the CountColumnsInTable function, because I can replace this line with:gridrow.Item("Columns in Database") = 9999
... and it makes no difference. I threw in .BeginEdit and .EndEdit in an attempt to get it to work, but these lines may as well not be there.Just an idea - is there any chance that you are trying to read past the end of the object bound to the datagrid. In some cases the first record in an object is 1 and in some cases it is 0 (I can't remember what is is in the case of a datagrid) I could be way off track here.
You always pass failure on the way to success.
-
Just an idea - is there any chance that you are trying to read past the end of the object bound to the datagrid. In some cases the first record in an object is 1 and in some cases it is 0 (I can't remember what is is in the case of a datagrid) I could be way off track here.
You always pass failure on the way to success.
Thanks for your thoughts. I managed to fix it. The problem was that I had created a sorted DataView, and populated the DataGridView with it - which had the effect of sorting one of the columns, which seemed to have messed up the indexing. One thing I can't get my head around is that the grid was bound to the dataview, so the indices should, I think, have worked - irrespective of the sort.