Domanda

My DataGridView has a fixed column (the first one of the grid's columns) called "Order", this column should have values of ordinal numbers from 1 to the count of rows of the DataGridView (1,2,3,4,...,n) and this order shouldn't be changed.

But when user clicks on another column's header for sorting, this may change the order of all the rows and the values in the order column will be changed accordingly. (for example, it can turn to n,...,4,3,2,1 or any other possible orders depend on what column user chooses to sort). This is weird to user. I have to repopulate the values for the order column to reset them to 1,2,3,4,...,n every time user clicks one of column header to sort. I wonder if there is a standard way for this? or a simpler one?

Your help would be highly appreciated!

Thank you!

È stato utile?

Soluzione

You can use DataGridView.Sorted Event.

Code example:

    private void dataGridView1_Sorted(object sender, System.EventArgs e)
    {
        foreach (DataGridViewRow dataGridViewRow in
                                 dataGridView1.Rows.Cast<DataGridViewRow>())
        {
            dataGridViewRow.Cells["Order"].Value = dataGridViewRow.Index + 1;
        }
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top