Home All Groups Group Topic Archive Search About

DateGridView NRE Error

Author
15 Apr 2010 10:26 AM
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?

Author
16 Apr 2010 8:07 AM
prodata
No thoughts anyone please?

JGD
Author
16 Apr 2010 9:27 AM
Andrew Morton
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
Author
16 Apr 2010 9:51 AM
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...)
Author
16 Apr 2010 10:16 AM
Andrew Morton
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
Author
16 Apr 2010 11:03 AM
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?)