When you see the title, you may think that this article is not much new. It is nothing more than forgetting to initialize a category of issues. However, we suggest you take a look at it. I guarantee that this issue is not so boring, hope to give you some fun!
You can reproduce this problem as follows:
1. Place a TabControl control on the form with two pages (tabPage1 and tabPage2.
2. Place a dview control dgv1 on tabPage1, A DataGridView control dvg2 on tabPage2, And the AutoSizeColumnsMode attribute of the two dview is Fill, after setting the data sources of the two dview S (you can set some data sources here to ensure that there is a column of data and you can also set arrays ).
3. subscribe to the Load event of the form. The event handler is as follows:
Private void form1_Load (object sender, EventArgs e)
{
Dgv1.Columns [0]. HeaderText = "name ";
Dgv1.Columns [0]. Width = 120;
Dgv2.Columns [0]. HeaderText = "name ";
Dgv2.Columns [0]. Width = 120; // NullReferenceException occurs when this sentence is run;
}
As shown in the preceding steps, this problem can be reproduced. The problem is very strange. When HeaderText is set, the NullReferenceException is not thrown. This indicates that dgv2.Columns [0] is not null and is observed in the monitoring window, it is indeed not null.
So what is null? You may think that I will use IL to explain the problem again!
This time we don't need to explain through IL. Let's look at the source code in the Class Library:
The Width attribute of the DataGridViewColumn class is defined as follows:
Public int Width
{
Get
{
Return this. Thickness;
}
Set
{
This. Thickness = value;
}
}
In fact, nothing is actually done, but the Thickness attribute of the base class DataGridViewBand is set. The Thickness attribute is defined as follows:
Internal int Thickness
{
Get
{
If (this. bandIsRow & this. bandIndex>-1)
{
Int height, minimumHeight;
GetHeightInfo (this. bandIndex, out height, out minimumHeight );
Return height;
}
Return this. thickness;
}
Set
{
Int minimumThickness = this. MinimumThickness;
If (value <minimumThickness)
{
Value = minimumThickness;
}
If (value> maxBandThickness)
{
If (this. bandIsRow)
{
Throw new ArgumentOutOfRangeException ("Height", SR. getString (SR. invalidHighBoundArgumentEx, "Height", (value ). toString (CultureInfo. currentCulture), (maxBandThickness ). toString (CultureInfo. currentCulture )));
}
Else
{
Throw new ArgumentOutOfRangeException ("Width", SR. getString (SR. invalidHighBoundArgumentEx, "Width", (value ). toString (CultureInfo. currentCulture), (maxBandThickness ). toString (CultureInfo. currentCulture )));
}
}
Bool setThickness = true;
If (this. bandIsRow)
{
If (this. DataGridView! = Null & this. DataGridView. AutoSizeRowsMode! = Maid. None)
{
This. cachedThickness = value;
SetThickness = false;
}
}
Else
{
Maid = (maid) this;
DataGridViewAutoSizeColumnMode inheritedAutoSizeMode = dataGridViewColumn. InheritedAutoSizeMode;
If (inheritedAutoSizeMode! = Maid &&
InheritedAutoSizeMode! = Maid. None &&
InheritedAutoSizeMode! = DataGridViewAutoSizeColumnMode. NotSet)
{
This. cachedThickness = value;
SetThickness = false;
}
Else if (inheritedAutoSizeMode = DataGridViewAutoSizeColumnMode. Fill & this. DataGridView! = Null)
{
If (maid. Visible)
{
IntPtr handle = this. DataGridView. Handle;
This. DataGridView. AdjustFillingColumn (dataGridViewColumn, value );
SetThickness = false;
}
}
}
If (setThickness & this. thickness! = Value)
{
If (this. DataGridView! = Null)
{
This. DataGridView. OnBandThicknessChanging ();
}
This. ThicknessInternal = value;
}
}
}
The code is too long. I post the Code related to this issue and set the AutoSizeColumnsMode attribute to Fill. It will be executed here:
Else if (inheritedAutoSizeMode = DataGridViewAutoSizeColumnMode. Fill & this. DataGridView! = Null)
{
If (maid. Visible)
{
IntPtr handle = this. dataGridView. handle; // I think the exception is thrown by this line of code. Because the TabPage of your dgv2 is not activated, Handle is null, so an NullReferenceException is thrown.
This. DataGridView. AdjustFillingColumn (dataGridViewColumn, value );
SetThickness = false;
}
}
This exception is thrown internally!
You can solve this problem as follows:
1. Do not set the AutoSizeColumnsMode attribute to Fill, and then set the column width by yourself.
2. You can set the TabPage of dgv2 to the active state:
Private void form1_Load (object sender, EventArgs e)
{
This. tabControl1.SelectedIndex = 1;
Dgv1.Columns [0]. HeaderText = "name ";
Dgv1.Columns [0]. Width = 120;
Dgv2.Columns [0]. HeaderText = "name ";
Dgv2.Columns [0]. Width = 120;
}
3. Modify the column width elsewhere, for example, in the DataBindingComplete event of dgv2.