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:
http://i50.tinypic.com/211m81x.png
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:
Public Sub UpdateTodoGrid()
' Load todo items
todoGrid.DataSource = _TodoManager.LoadAll()
' Set backcolor
For Each row As DataGridViewRow In todoGrid.Rows
If CBool(row.Cells("Completed").Value) Then
row.DefaultCellStyle.BackColor = _CompletedCellBackColor
Else
row.DefaultCellStyle.BackColor = Color.White
End If
Next
' Hide a few columns
Dim visibleColumns As New List(Of String) From {"Name", "Category", "Completed", "Priority"}
For Each c As DataGridViewColumn In todoGrid.Columns
c.Visible = visibleColumns.Contains(c.Name)
c.ReadOnly = True
Next
' Set the Completed column first and make it smaller
Dim col = todoGrid.Columns("Completed")
col.DisplayIndex = 0
col.HeaderText = String.Empty
col.Width = 32 ' <--- setting width here works just fine
col.ReadOnly = False
End Sub
And here's the code for the second grid:
vb.net Code:
Public Sub UpdateCategoryGrid()
' Load categories
categoryGrid.DataSource = _CategoryManager.LoadAll()
' Hide a few columns
Dim visibleColumns As New List(Of String) From {"Name", "Description", "Color"}
For Each c As DataGridViewColumn In categoryGrid.Columns
c.Visible = visibleColumns.Contains(c.Name)
Next
' Set the Color column first and make it smaller
Dim col = categoryGrid.Columns("Color")
col.DisplayIndex = 0
col.HeaderText = String.Empty
col.Width = 32 ' <--- null reference here...?
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 :ehh:
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.
Re: DataGridView - null reference on setting column width..?
You can try to comment the line where do you define the DisplayIndex, in the past i had some problems when using that property and the AutoGeneratedColumns...
Re: DataGridView - null reference on setting column width..?
Quote:
Originally Posted by
mickey_pt
You can try to comment the line where do you define the DisplayIndex, in the past i had some problems when using that property and the AutoGeneratedColumns...
You mean just leave out the DisplayIndex? Nope, doesn't work, same problem.