【问题标题】:reordering rows in a databound datagridview重新排序数据绑定数据网格视图中的行
【发布时间】:2009-08-25 01:13:09
【问题描述】:

我有一个绑定到 DataTable 的 DataGridView,有没有办法重新排序 DataGridView 中的行并将更改反映在绑定的 DataTable 中?

【问题讨论】:

    标签: winforms datagridview


    【解决方案1】:

    DataTableDataGridView 之间使用DataViewBindingSource,它们都有Sort 属性:

    DataView view = new DataView(table);
    view.Sort = "Name ASC, Age Desc";
    dataGridView.DataSource = view;
    

    您还可以使用DataView.RowFilter 属性(或BindingSource.Filter)过滤结果。

    DataGridView 将自动反映更改

    【讨论】:

    • 如果没有明确的排序方案怎么办? IE。我想将第 9 行移到第 1 位?
    • 删除DataTable的第九行,插入到第一个位置
    • - 或向 DataTable 添加一个新的“Order”列并将其用于排序。无论您做什么,始终在 DataSource 端执行排序,而不是在 DataGridView 中执行排序,否则您最终会上吊;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多