How to freeze first row in Datagridview when using a custom SortComparer?
Without SortComparer row[x].froozen = true; would do it.
But with a SortComparer it doesnt work
Here is my SortComparer Code:
DataGridView dg = (DataGridView)sender;
if (e.Column.Index == 0)
{
e.SortResult = System.String.Compare(e.CellValue1.ToString(), e.CellValue2.ToString());
if (e.SortResult == 0)
{
e.SortResult = System.String.Compare(
dg.Rows[e.RowIndex1].Cells[1].Value.ToString(),
dg.Rows[e.RowIndex2].Cells[1].Value.ToString());
}
e.Handled = true;
}
else if (e.Column.Index == 1)
{
e.SortResult = System.String.Compare(e.CellValue1.ToString(), e.CellValue2.ToString());
if (e.SortResult == 0)
{
e.SortResult = System.String.Compare(
dg.Rows[e.RowIndex1].Cells[0].Value.ToString(),
dg.Rows[e.RowIndex2].Cells[0].Value.ToString());
}
e.Handled = true;
}
You could extend your SortComparer to check if the first row index is the top row or not. If it is, set the
e.SortResult = 0or else run whatever code you want to sort with.or you can make it more fancy by checking if the row is frozen or not:
EDIT the following does not work since the Frozen property of a Row gets set to false by default for some reason
EDIT The
e.SortResult = 0instead of -1 as rows that are equal will be sorted based on their current position.