DateGridView NRE Error

  • Thread starter Thread starter prodata
  • Start date Start date
P

prodata

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?
 
prodata said:
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
 
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...)
 
Sorry but no - still gives the same error.

But I've tried a slightly different tack of setting the .MinimumWidth
property rather than .Width (which should still do what I need to do).

If I set the .MinimumWidth = 100 ' (px)

then all runs fine. But if I set:

..MinimumWidth = 120

then the NRE exception gets thrown!

Bizarre! (But I'm having to use VB2005 currently and so this
(presumed) bug was maybe fixed in VB2008/2010?)
 
Back
Top