如何在使用c#编辑winforms中的列值时对DataGridView进行排序

时间:2011-12-12 12:13:01

标签: c# winforms datagridview

我想对datagridview进行排序。当用户从此事件CellValueChanged对datagrid进行一些更改时。 在CellValueChanged完成后,datagridview应根据该列进行排序。我已将datagrid与list绑定。

1 个答案:

答案 0 :(得分:1)

您可以使用ColumnIndexDataGridViewCellEventArgs

sort属性
    public Form1()
    {
        InitializeComponent();

        dataGridView1.CellValueChanged +=new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);

        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("col1"));
        dt.Columns.Add(new DataColumn("col2"));
        dt.Columns.Add(new DataColumn("col3"));

        var r1 = dt.NewRow();
        r1["col1"] = "a1";
        r1["col2"] = "b1";
        r1["col3"] = "c1";

        var r2 = dt.NewRow();
        r2["col1"] = "a2";
        r2["col2"] = "b2";
        r2["col3"] = "c2";

        var r3 = dt.NewRow();
        r3["col1"] = "a3";
        r3["col2"] = "b3";
        r3["col3"] = "c3";

        dt.Rows.Add(r1);
        dt.Rows.Add(r2);
        dt.Rows.Add(r3);

        BindingSource bs = new BindingSource();
        bs.DataSource = dt;

        dataGridView1.DataSource = bs;

    }

    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        var dataGrid = (DataGridView)sender;
        var dataGridColumn = dataGrid.Columns[e.ColumnIndex];
        dataGrid.Sort(dataGridColumn, ListSortDirection.Ascending);
    }