There are a number of ways.
1, can be in DataGridView control in the rowstatechanged event changes the value of the row header cell (Row.HeaderCell.Value)
1 /// <summary>2 ///occurs when a row state changes3 /// </summary>4 /// <param name= "Sender" ></param>5 /// <param name= "E" ></param>6 Private voidDatagridview1_rowstatechanged (Objectsender, DataGridViewRowStateChangedEventArgs e)7{
8 //E.row.headercell.value = string. Format ("{0}", E.row.index + 1);9E.row.headercell.value = (E.row.index +1). ToString ();//Add line numberTen}
2. The rowpostpaint Event Example can be set in the DataGridView control, and theDrawText () method of the TextRenderer class uses the specified device context, font, The color and format description draws the specified text in the specified bounds.
1 /// <summary>2 ///occurs when a row is drawn when all cells are drawn3 /// </summary>4 /// <param name= "Sender" ></param>5 /// <param name= "E" ></param>6 Private voidDatagridview1_rowpostpaint (Objectsender, Datagridviewrowpostpainteventargs e)7 {8 //9System.Drawing.Rectangle Rectangle =NewRectangle (e.rowbounds.location.x, E.rowbounds.y, This. Datagridview1.rowheaderswidth-4, This. datagridview1.columnheadersheight);TenTextrenderer.drawtext (E.graphics, (E.rowindex +1). ToString (), This. DataGridView1.RowHeadersDefaultCellStyle.Font, Rectangle, This. DataGridView1.RowHeadersDefaultCellStyle.ForeColor, Textformatflags.verticalcenter | Textformatflags.right);//"Textformatflags.verticalcenter | Textformatflags.right "in" | " Has an added effect, two text character formatting styles are added here One}
Adding row headings to DataGridView controls in Winform