【问题标题】:Fix the last row of a DataGridView even if someone clicked the column header to sort the list即使有人单击列标题对列表进行排序,也修复 DataGridView 的最后一行
【发布时间】: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


【解决方案1】:

我知道这是一个老问题,但这是我想出的解决方案。使用网格上的 SortCompare 事件来覆盖特定行的排序:

    private void dgvData_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
    {
        if (dgvData.Rows[e.RowIndex1].Cells[0].Value.ToString() == "Total" ||
            dgvData.Rows[e.RowIndex2].Cells[0].Value.ToString() == "Total")
        {
            if (dgvData.SortOrder == SortOrder.Ascending)
            {
                e.SortResult = -1;
            }
            else
            {
                e.SortResult = 1;
            }
            e.Handled = true;
        }
    }

现在,第一列中包含“总计”的任何行都将始终排在网格的末尾。

(如果您允许对列重新排序,那么您需要弄清楚如何检查正确的列)

【讨论】:

    猜你喜欢
    • 2016-10-26
    • 2011-10-10
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    相关资源
    最近更新 更多