Search code examples
vb.netdatagridviewnullreferenceexceptiondatagridviewcolumn

DataGridView column names not accessable after adding new column


I'm trying to add a column to an existing data grid view and I'm getting an error after adding a column. Referencing the column by it's name throws a null reference exception and after some debugging I noticed, the names of the columns have disappeared after adding a column.

Before I add a new column you can see in the first image, that each column has a name. After I add a column, the second image shows the names of each column as being empty. No code was changed and the column being added to the data grid view was the only change.

DGV_List.Columns("Vendor").Visible = CB_Vendor.Checked

Normal operation Error after adding column

I've found one or two ways to get around this, such as directly referencing the column like Me.Vendor.Visible. But I'm curious why adding a new column could cause existing working code to fail.

Edit adding code

The code is really long so I'm linking it on pastebin. Also note the column was added manually and not with code. Nothing besides the form designer code changed when the error started.

Code that has null reference error after adding column

https://pastebin.com/inZCT27A

Form designer before adding column

https://pastebin.com/2nv33pA3

Form design after adding column

https://pastebin.com/7ULHpNwE


Solution

  • I am pretty sure that the problem lies in “WHEN” the CheckBoxs CheckedChanged event is fired.

    If the check box is set to True in the designer, then, its CheckedChanged event will get fired "once" in the InitializeComponent method.

    If the check box is set to False in the designer, then its CheckedChanged event will NOT be fired in the InitializeComponent method.

    Because the check box is set to true in the designer, then "sometime" in the InitializeComponent method the CheckChanged event is going to get fired and when it does... you can NOT guarantee that the grid has been fully initialized.

    Checking “anything” relating to the grid “before” it has been fully initialized is risky. This would easily explain some of the inconsistencies I and I am sure you have seen.

    The main point would be that, because you DO want to reference the grid in the CheckChanged event of the check boxes, AND you DO want to have the check box initially checked, then you need to make sure the grid is fully initialized "before" you set the check box to True/False (checked/unchecked).

    One way to do this is from the “designer”, UNCHECK the check boxes that reference the grid. This will prevent the CheckedChanged event from firing in the InitializeComponent method when the grid may not be fully initialized.

    Then in the forms Load event, where we can pretty much guarantee the grid is initialized fully, set the check boxes state to checked. Then the CheckedChanged event can fire without errors. …

    Private Sub MCRI_Checker_Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      CB_Description.Checked = True
      CB_Drawing.Checked = True
      CB_FilePath.Checked = True
      CB_Quantity.Checked = True
      CB_ReqType.Checked = True
      CB_Vendor.Checked = True
      CB_WhereUsed.Checked = True
    End Sub
    

    I hope this helps and makes sense.