|
-
Jul 20th, 2007, 09:29 AM
#1
Thread Starter
Addicted Member
[RESOLVED] [2005] DataGridView throws error when scrolled to bottom
I am getting an object reference not set to an instance of an object error when I scroll to the bottom of a DataGridView.
The grid has three columns; File Name; Columns; Database Columns.
I populate the grid by extracting the contents of a zip file, so I show the File name in the zip, the number of columns in the (pipe delimited) text file, and to start with, null in the "Database Columns" column.
Everything is fine up until this point. I can scroll up and down with no problems.
I then iterate through a TreeView, collect node values, and compare these values with the "File Name" column in the grid. If there is a match, the "Database Columns" column is filled in - and I can see the desired results.
What I find is that when I now scroll to the bottom of the grid, it thows an error:
Code:
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Data.DataView.IsOriginalVersion(Int32 index)
at System.Data.DataRowView.GetColumnValue(DataColumn column)
at System.Data.DataColumnPropertyDescriptor.GetValue(Object component)
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetValue(Int32 boundColumnIndex, Int32 columnIndex, Int32 rowIndex)
at System.Windows.Forms.DataGridViewCell.GetValue(Int32 rowIndex)
at System.Windows.Forms.DataGridViewCell.PaintWork(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
at System.Windows.Forms.DataGridViewRow.PaintCells(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow, DataGridViewPaintParts paintParts)
at System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
at System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
at System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
at System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
And the space occupied by the DataGridView changes to show a huge red square in it.
Does the above mean anything to anybody?
-
Jul 20th, 2007, 09:50 AM
#2
Fanatic Member
Re: [2005] DataGridView throws error when scrolled to bottom
Did you use the New keyword when declaring the variable for the dataset?
Using Visual Studio 2008
Please mark your thread RESOLVED if you no longer need help.
-
Jul 20th, 2007, 10:19 AM
#3
Thread Starter
Addicted Member
Re: [2005] DataGridView throws error when scrolled to bottom
Yes - I'm using New when creating the DataSet.
I've concluded that it must be something to do with the following:
Code:
If bindingDataGridView2.Find("File Name", strSearch) <> -1 Then
intItemFoundOnRow = bindingDataGridView2.Find("File Name", strSearch)
Dim gridrow As DataRowView = bindingDataGridView2.Item(intItemFoundOnRow)
gridrow.Item("Columns in Database") = CountColumnsInTable(strTableName)
End If
It isn't anything to do with the CountColumnsInTable function because I can change this line to:
Code:
gridrow.Item("Columns in Database") = 2222
And I still have the problem - so it must be something to do with the way I'm updating the 'cell'.
I have tried gridrow.beginedit and .endedit as I thought this might be the problem, but it made no difference.
-
Jul 20th, 2007, 04:33 PM
#4
Re: [2005] DataGridView throws error when scrolled to bottom
Just a stab in the dark...
But might it be something to do row index numbers? Maybe you are trying to get the row with an index equal to the number of rows, where in actual fact the index should be 'number of rows - 1'.
I am assuming that the first row will be index 0.
Just a thought!
-
Jul 23rd, 2007, 09:08 AM
#5
Thread Starter
Addicted Member
Re: [2005] DataGridView throws error when scrolled to bottom
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|