Hide blank columns:
[Csharp]
Maid = false;
Maid = false;
The width of the blank column cannot be changed:
[Csharp]
This. dgv. RowHeadersWidthSizeMode = System. Windows. Forms. Maid;
This. dgv. RowHeadersWidthSizeMode = System. Windows. Forms. Maid;
To display the number of rows in a blank column:
[Csharp]
Private void dgv_RowPostPaint (object sender, DataGridViewRowPostPaintEventArgs e)
{
// # Region method 1
// Using (SolidBrush B = new SolidBrush (dgv. RowHeadersDefaultCellStyle. ForeColor ))
//{
// Int linen = 0;
// Linen = e. RowIndex + 1;
// String line = linen. ToString ();
// E. Graphics. DrawString (line, e. InheritedRowStyle. Font, B, e. RowBounds. Location. X, e. RowBounds. Location. Y + 5 );
// SolidBrush B = new SolidBrush (Color. Red );
//}
// # Endregion
# Region method 2
Rectangle rectangle = new Rectangle (e. RowBounds. Location. X, e. RowBounds. Location. Y, dgv. RowHeadersWidth-4, e. RowBounds. Height );
TextRenderer. DrawText (
E. Graphics, (e. RowIndex + 1). ToString (),
Dgv. RowHeadersDefaultCellStyle. Font,
Rectangle,
Dgv. RowHeadersDefaultCellStyle. ForeColor,
TextFormatFlags. VerticalCenter | TextFormatFlags. HorizontalCenter
);
# Endregion
}
Private void dgv_RowPostPaint (object sender, DataGridViewRowPostPaintEventArgs e)
{
// # Region method 1
// Using (SolidBrush B = new SolidBrush (dgv. RowHeadersDefaultCellStyle. ForeColor ))
//{
// Int linen = 0;
// Linen = e. RowIndex + 1;
// String line = linen. ToString ();
// E. Graphics. DrawString (line, e. InheritedRowStyle. Font, B, e. RowBounds. Location. X, e. RowBounds. Location. Y + 5 );
// SolidBrush B = new SolidBrush (Color. Red );
//}
// # Endregion
# Region method 2
Rectangle rectangle = new Rectangle (e. RowBounds. Location. X, e. RowBounds. Location. Y, dgv. RowHeadersWidth-4, e. RowBounds. Height );
TextRenderer. DrawText (
E. Graphics, (e. RowIndex + 1). ToString (),
Dgv. RowHeadersDefaultCellStyle. Font,
Rectangle,
Dgv. RowHeadersDefaultCellStyle. ForeColor,
TextFormatFlags. VerticalCenter | TextFormatFlags. HorizontalCenter
);
# Endregion
}
If you are using the Table-bound Gridview, we recommend that you use the following method:
[Csharp]
// Implement the function to add an automatic number for the DataGridView
DataTable table = new DataTable ();
DataColumn column = new DataColumn ();
Column. AutoIncrement = true; // AutoIncrement gets or sets a value that indicates whether the column value is automatically incremented for new rows added to the table.
Column. ColumnName = "auto number ";
Column. AutoIncrementSeed = 1;
Column. AutoIncrementStep = 1;
Table. Columns. Add (column );
Table. Merge (table); // Merge merges the DataTable
This. Maid = table