设置 datagridview 的最后一行不可排序(始终位于底部)
我有一个可排序的 DatagridView,其摘要最后一行包含一些列的总和,我想将此摘要行始终保留为 datagridView 的最后一行(底部)。
目前,当我对 datagridView 的列进行排序时,摘要行也会进行排序,我不希望这样。我想保留 DatagridView 的最后一行(摘要行)不可排序。
有办法做到吗?
我找到了解决方案:
我以这种方式重载了 sortcompare 方法:
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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以向数据表添加一列,并将所有非汇总记录的值设置为 0。将汇总记录值设置为 1,然后对“汇总”列进行排序,然后对其他列进行排序。
这在内存中可能表现不佳,因此如果可能的话,您希望在 SQL 查询中执行此操作,具体取决于您使用的数据库。
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.