Jun 9, 2010
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. 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...
View 2 Replies
Here's the code for the first grid (working fine):
Public Sub UpdateTodoGrid()
' Load todo items
todoGrid.DataSource = _TodoManager.LoadAll()
' Set backcolor
For Each row As DataGridViewRow In todoGrid.Rows
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. 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.
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. 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.