When C # is used for development, the DataGridView display row numbers in HeaderCell,
When using C # To develop the WinForm program, the DataGridView is a table data control that we often use. When using this control, you often need to display the row number in the first column to know which row to operate.
At this time, we have two display methods:
One is directly displayed in HeaderCell.
The row number of the DataGridView is displayed on the HeaderCell.
The other is displayed in the data column.
The row number of the DataGridView instance is displayed on the data column.
The disadvantage of displaying data columns is obvious. Generally, the data displayed in the DataGridView has a dataset, and one column must be occupied in the dataset, adding a row number that is irrelevant to the original dataset to the dataset violates the principle of high cohesion and low coupling. More importantly, if a dataset needs to insert or delete a row, all affected row numbers must be modified and then rendered to the dview. this is inconvenient, especially when the data volume is large, the impact is even greater.
Therefore, the method shown in HeaderCell is used here.
Step 1
Select the DataGridView control, click the event button, find RowStateChanged, double-click the blank text box on the right, automatically bind the event and generate a method.
RowStateChanged event bound to the DataGridView
Write the following code in the method:
Private void maid (object sender, maid e)
{
// Displayed on HeaderCell
For (int I = 0; I <this. dataGridView1.Rows. Count; I ++)
{
DataGridViewRow r = this. dataGridView1.Rows [I];
R. HeaderCell. Value = string. Format ("{0}", I + 1 );
}
This. Maid ();
}
The HeaderCell data in the DataGridView is incomplete.
Step 2
The row number is not fully displayed. The solution is to set the RowHeadersWidthSizeMode attribute of the DataGridView to AutoSizeToAllHeaders, AutoSizeToDisplayedHeaders, or AutoSizeToFirstHeader.
The RowHeadersWidthSizeMode attribute of the DataGridView is set to AutoSizeToAllHeaders, AutoSizeToDisplayedHeaders, or AutoSizeToFirstHeader.