【发布时间】:2018-01-11 13:19:39
【问题描述】:
Table1 Fig.
Name | Marks
Pritam | 80
Aruna | 85
Uttaran | 90
Total | 255
DataTable dtStudentInfo = table1;
dataGridView1.DataSource = dtStudentInfo;
点击列标题名称后datagridview按学生姓名升序排列。但我希望 Total 行始终保持在列表的最后。
我想知道是否有任何方法可以从将要排序的列表中删除最后一行。如果这是不可能的,那么建议我可以通过任何方式获得结果。注意:我不希望任何外部按钮对列表进行排序。
我通过以下方式解决了这个问题:
DataGridViewRow dgRowTotalCount;
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
{
dgRowTotalCount = (DataGridViewRow)dataGridView1.Rows[((DataGridView)sender).Rows.Count - 1].Clone();
for (Int32 index = 0; index < ((DataGridView)sender).Rows[((DataGridView)sender).Rows.Count - 1].Cells.Count; index++)
{
dgRowTotalCount.Cells[index].Value = ((DataGridView)sender).Rows[((DataGridView)sender).Rows.Count - 1].Cells[index].Value;
}
((DataGridView)sender).Rows.RemoveAt(((DataGridView)sender).Rows.Count - 1);
}
}
private void dataGridView1_Sorted(object sender, EventArgs e)
{
DataTable dtDGVCopy = new DataTable();
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
dtDGVCopy.Columns.Add(col.Name);
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataRow dRow = dtDGVCopy.NewRow();
foreach (DataGridViewCell cell in row.Cells)
{
dRow[cell.ColumnIndex] = cell.Value;
}
dtDGVCopy.Rows.Add(dRow);
}
dtDGVCopy.Rows.Add();
for (Int32 i = 0; i < dgRowTotalCount.Cells.Count - 1; i++)
{
dtDGVCopy.Rows[dtDGVCopy.Rows.Count-1][i] = dgRowTotalCount.Cells[i].Value;
}
dataGridView1.DataSource = null;
dataGridView1.DataSource = dtDGVCopy;
}
但它不像以前那样光滑。如果有什么方法可以让它像以前一样表现,那就太好了。
【问题讨论】:
-
您可以在该特定行上使用 .Frozen = true
-
这行不通。我试过了。
-
对不起,我帮不上忙!
-
如果打开了 edit = true 选项,您将在 datagridview 中获得额外的一行。这是为了允许某人添加新数据。关闭编辑选项将删除最后一个空行。
-
当然,您应该寻找“带页脚的 DataGridView”。 For example.
标签: c# visual-studio sorting datagridview