Results 1 to 5 of 5

Thread: [RESOLVED] [2005] DataGridView throws error when scrolled to bottom

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Resolved [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?
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  2. #2
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    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.
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  4. #4
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    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!
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    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.
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width