DateGridView NRE Error

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?
 
A

Andrew Morton

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
 
P

prodata

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...)
 
P

prodata

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?)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top