C# DataGridView 添加行号

C# DataGridView 添加行号

  private void dgvItem_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
  {
      if (e.RowIndex >= 0 && e.ColumnIndex == -1)
      {
          e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground);
          using (Brush brush = new SolidBrush(e.CellStyle.ForeColor))
          {
              e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.CellStyle.Font, brush, e.CellBounds.Location.X + 10, e.CellBounds.Location.Y + 4);
          }
          e.Handled = true;
      }
  }

你可能感兴趣的:(c#,开发语言)