C# – Prevent DataGridView selecting a row when sorted if none was previously selected

cdatagridviewwinforms

I have a datagridview that may or may not have rows selected when the user sorts it by clicking on a column header. If there are rows selected there isn't a problem, but if there are 0 selected rows then the sort selects a row automatically (the selection is consistant but I'm not wure what the criteria is). How can I prevent this behavior from happening.

If it's relevant, the DGV is not databound and has full row select enabled.

Best Answer

Handle the Sorted event of the DataGridView:

this.dataGridView1.Sorted += new System.EventHandler(dataGridView1_Sorted);

void dataGridView1_Sorted(object sender, System.EventArgs e)
{
    dataGridView1.ClearSelection();
}
Related Topic