Results 1 to 3 of 3

Thread: DataGridView - null reference on setting column width..?

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    DataGridView - null reference on setting column width..?

    Hey,

    I have a databound DataGridView, and I'd like to keep one column in front, with a slightly smaller width than the default. The grid's AutoSizeColumnsMode property is set to Fill so that the columns fill out the entire grid. This makes the first column equally large as all others, which is why I need to set its width manually.

    Here's a screenshot so you know what I'm talking about:

    The smaller column is the first one with the checkboxes.


    I have two grids that I need to do this with, and for some reason it works just fine with the first grid, but gives a null reference error on the second grid...

    Here's the code for the first grid (working fine):
    vb.net Code:
    1. Public Sub UpdateTodoGrid()
    2.         ' Load todo items
    3.         todoGrid.DataSource = _TodoManager.LoadAll()
    4.  
    5.         ' Set backcolor
    6.         For Each row As DataGridViewRow In todoGrid.Rows
    7.             If CBool(row.Cells("Completed").Value) Then
    8.                 row.DefaultCellStyle.BackColor = _CompletedCellBackColor
    9.             Else
    10.                 row.DefaultCellStyle.BackColor = Color.White
    11.             End If
    12.         Next
    13.  
    14.         ' Hide a few columns
    15.         Dim visibleColumns As New List(Of String) From {"Name", "Category", "Completed", "Priority"}
    16.         For Each c As DataGridViewColumn In todoGrid.Columns
    17.             c.Visible = visibleColumns.Contains(c.Name)
    18.             c.ReadOnly = True
    19.         Next
    20.  
    21.         ' Set the Completed column first and make it smaller
    22.         Dim col = todoGrid.Columns("Completed")
    23.         col.DisplayIndex = 0
    24.         col.HeaderText = String.Empty
    25.         col.Width = 32            ' <--- setting width here works just fine
    26.         col.ReadOnly = False
    27.     End Sub

    And here's the code for the second grid:
    vb.net Code:
    1. Public Sub UpdateCategoryGrid()
    2.         ' Load categories
    3.         categoryGrid.DataSource = _CategoryManager.LoadAll()
    4.  
    5.         ' Hide a few columns
    6.         Dim visibleColumns As New List(Of String) From {"Name", "Description", "Color"}
    7.         For Each c As DataGridViewColumn In categoryGrid.Columns
    8.             c.Visible = visibleColumns.Contains(c.Name)
    9.         Next
    10.  
    11.         ' Set the Color column first and make it smaller
    12.         Dim col = categoryGrid.Columns("Color")
    13.         col.DisplayIndex = 0
    14.         col.HeaderText = String.Empty
    15.         col.Width = 32      ' <--- null reference here...?
    16.     End Sub
    As you can see, the code is exactly the same (except for not setting the BackColor of each row and not using the ReadOnly property*).

    When I run this code, it gives me a null reference error ("Object reference not set to an instance of an object.") on the line where I set the column's Width.

    I have no idea what's causing this... Obviously 'col' is not Nothing (otherwise it would have errored before that line), and there's nothing else there that could be Nothing... I assume that something happens internally in the grid when you set the width of a column, and that there is a null reference there, but obviously I have no control over that so I've no idea how to fix this

    The grid's are absolutely the same, as the second grid is just a 'copy' (meaning I copy/pasted it in the designer) of the first. So all their properties are equal.


    What could be causing this..?


    *I thought maybe the ReadOnly properties were the problem, but I tried putting them in in the same way and the issue did not go away... The first grid is not ReadOnly (because the checkboxes need to be editable), but all rows except the first are, while the second grid is completely ReadOnly.

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