It is possible to sort a DataGridView
by clicking on one of its ColumnHeaderCell
. But I would like one column, containing the index of each row, to remain fixed.
What I mean is that when I click on another column header the grid rows should be sorted on the content of that column, all except the first column that contains the index of rows.
Sorry! I can't post image!
If you mean that you want to break the rows: No you can't do that without re-creating them completely. A Row is a unit with its Columns and they can only move as one.
However if you want one column to always contain the visible row number you'll simply have to re-fill that column after each sort:
private void dataGridView1_Sorted(object sender, EventArgs e)
{
dataGridView1.SuspendLayout();
int yourindex = 0; // whichever column your index is at
foreach (DataGridViewRow row in dataGridView1.Rows)
row.Cells[yourindex].Value = row.Index;
dataGridView1.ResumeLayout();
}
Note: By doing this you will lose the ability to re-create the original sort order. This may not be a problem. If it is you need to include an extra column called 'key' or 'old order' or whatever.