From: prodata on
I'm trying to use a DGV bound to a datatable as the data source that's
throwing a NullReferenceException (under VB2005) that I don't
understand. Here's the relevant block of code

With DataGridView1
.DataSource = dtRawLeWLData
.DefaultCellStyle.NullValue = "-"
.Columns("DirX").Visible = False
.Columns("DirY").Visible = False
.Columns("DateTime").Width = 30 ' (HERE'S THE PROBLEM
LINE)
End With

If I comment out the line setting the width of column
"DateTime" (which is a valid column name) then the code runs
perfectly. But with the line left in there's an NRE error. But the
lines above the Width setting one are presumably referencing the same
object so why does trying to set the column width throw the NRE error?
From: prodata on
No thoughts anyone please?

JGD
From: Andrew Morton on
prodata wrote:
> No thoughts anyone please?

You could try changing the name of the "DateTime" column in case it somehow
conflicts with the name of the DateTime structure. Or list what the computer
"thinks" the columns are called and see if it agrees with what you think it
should be. Or use the number of the column.

Andrew


From: prodata on
Thanks but I've previously tried using the column number instead of
name and it makes no difference.

I was wondering whether trying to set the column width is somehow a
true runtime instruction (ie active when the DGV is actually being
drawn) whereas things like setting the visibility of individual
columns is just setting a pre-draw-time property. So what I might need
to do is to try and set the column width at some later point in the
DGV's life-cycle, but I'm not sure how to do this. (No I don't think
it's a very convincing idea either, but I'm struggling...)
From: Andrew Morton on
prodata wrote:
> Thanks but I've previously tried using the column number instead of
> name and it makes no difference.

If you split up the assignment like they do in the example at
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.width.aspx

does it narrow it down?

Andrew