I have a sortable DatagridView with a summary last row containing sum of some columns, i want to keep this summary row always as the last line(bottom) of datagridView.
At the moment, when i sort a column of datagridView also the summary row get sorted, i don’t want this. I want to keep the last row (summary row) of DatagridView not sortable .
Is there a way to do it ?
I’ve found a solution :
I’ve overloaded the sortcompare method in this manner :
private void grid_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
try
{
if (e.RowIndex1 == this.dataGridView1.Rows.Count -1)
e.Handled = true;
if (e.RowIndex2 == this.dataGridView1.Rows.Count - 1)
e.Handled = true;
return;
}
catch (Exception ex)
{
ex.ToString();
}
}
You could add a column to the datatable, and set the value for all non-summary records to 0. Set the summary record value to 1, then sort on the “summary” column, then others.
This might not perform well in memory, so you’d want to do it in the SQL query if possible, depending on the database you use.