Hide blank columns:
dataGridView1.RowHeadersVisible = false;
The width of the blank column cannot be changed:
this.dgv.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing;
To display the number of rows in a blank column:
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 = 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:
// Implement function datagridview dview add automatic number 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 by column for new rows added to the table. columnname = "auto number"; column. autoincrementseed = 1; column. autoincrementstep = 1; table. columns. add (column); table. merge (table); // merge merges the datatable this. datagridview1.datasource = table;