简体   繁体   中英

Not Changeable Row number column in datagridview

I want to extend a dategridview with a (read only) column just for row number. The ROW NUMBER row`s order should not change when datagridview sort by other column content (Like excel)! is possible?

We can enumerate each row in one of two ways:

  • Adding a new column.
  • Within the row header.

Displaying in Added Column

private void AddIndexCol()
{
  DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
  col.Name = "Index";
  col.HeaderText = "Index";
  col.ReadOnly = true;
  col.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;

  DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
  cell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
  col.CellTemplate = cell;

  this.dataGridView1.Columns.Insert(0, col);
}

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
  if (e.ColumnIndex == 0)
  {
    e.Value = String.Format("{0}", e.RowIndex + 1);
    e.FormattingApplied = true;
  }
}

Credit to ASh for the CellFormatting code.


Displaying in RowHeader

public Form1()
{
  InitializeComponent();

  DataGridViewCellStyle style = new DataGridViewCellStyle();
  style.Alignment = DataGridViewContentAlignment.MiddleCenter;
  this.dataGridView1.RowHeadersDefaultCellStyle = style;
  this.dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;
}

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
  DataGridViewRowHeaderCell header = this.dataGridView1.Rows[e.RowIndex].HeaderCell;

  if (e.ColumnIndex == 0) // (header.Value == null)
  {
    header.Value = String.Format("{0}", e.RowIndex + 1);
  }
}

Note about the if statement. The condition e.ColumnIndex == 0 will always preserve numeric order through sorting while the condition header.Value == null will preserve row numbers with the original row (but will need additional code when handling row deletion). For example, this descending sort:

  Col == 0           Header == null
1 a  =>  1 c          1 a  =>  3 c
2 b      2 b          2 b      2 b 
3 c      3 a          3 c      1 a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM