datagridview列排序后,MoveUp / MoveDown行不起作用

时间:2011-06-06 12:54:43

标签: c# data-binding sorting datagridview

我将以下DataGridView与bindingSource相关联:

bindingSource1.DataSource = dataset1;
bindingSource1.DataMember = "table1";
dataNavigator1.DataSource = bindingSource1;
dataGridView1.DataSource = bindingSource1;

我有两个功能可以上下移动选定的行:

private void buttonUp_Click(object sender, EventArgs e)
{
    int index = this.bindingSource1.Position;

    if (index > 0)
    {
        DataRow dr = (DataRow)this.dataset1["table1"].Rows[index];
        DataRow newDr = this.dataset1["table1"].NewRow();
        newDr.ItemArray = dr.ItemArray;
        this.dataset1["table1"].Rows.RemoveAt(index);
        this.dataset1["table1"].Rows.InsertAt(newDr, index - 1);
        this.bindingSource1.Position = index - 1;
    }
}

private void buttonDown_Click(object sender, EventArgs e)
{
    int index = this.bindingSource1.Position;

    if (index < this.bindingSource1.Count)
    {
        DataRow dr = (DataRow)this.dataset1["table1"].Rows[index];
        DataRow newDr = this.dataset1["table1"].NewRow();
        newDr.ItemArray = dr.ItemArray;
        this.dataset1["table1"].Rows.RemoveAt(index);
        this.dataset1["table1"].Rows.InsertAt(newDr, index + 1);
        this.bindingSource1.Position = index + 1;
    }
}

这两种方法工作正常,当我点击按钮移动行时,它会被正确移动。

但是,如果我之前在列上单击以对其进行排序(单击Header)并在尝试再次移动一行之后,它会移动绑定源位置但不会移动datagridview中的行。 我调试函数并没有出错,它似乎只是一个datagridview可视化错误。 我试图在dataGridView1_Sorted处理事件中扩展绑定源的排序,但仍然不起作用。 为什么在对datagridview进行排序操作后没有移动行?

感谢。 -Alessandro

我取得了一些进展,但点击标题网格列排序后仍然存在一些问题。我试图重置bindingSource1.Sort =“”;在移动行功能,行和现在行移动但位置错误!!这里是代码,所以你自己尝试..

public partial class Form1 : Form
{
    DataTable dt;
    DataSet ds = new DataSet();

    public Form1()
    {
        InitializeComponent();
        dt = new DataTable("table1");
        dt.Columns.Add("Column1", typeof(int));
        dt.Columns.Add("Column2", typeof(int));
        dt.Columns.Add("Column3", typeof(int));
        dt.Rows.Add(1, 0, 0);
        dt.Rows.Add(2, 1, 1);
        dt.Rows.Add(2, 0, 0);
        dt.Rows.Add(3, 1, 1);
        dt.Rows.Add(3, 0, 4);
        dt.Rows.Add(3, 3, 4);
        ds.Tables.Add(dt);

        bindingSource1.DataSource = ds.Tables[0];
        this.dataGridView1.DataSource = bindingSource1;
    }

    private void dataGridView1_Sorted(object sender, EventArgs e)
    {
        //force the BindingSource to reflect the same sort order as the DataGridView
        String sort = dataGridView1.SortedColumn.DataPropertyName;

        if (dataGridView1.SortOrder == SortOrder.Descending)
        {
            sort = sort + " DESC";
        }

        //ds.Tables["table1"].DefaultView.Sort = sort;
        bindingSource1.Sort = sort;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        bindingSource1.Sort = "";
        //ds.Tables[0].DefaultView.Sort = "";

        int index = this.bindingSource1.Position;

        if (index > 0)
        {
            DataRow dr = (DataRow)this.ds.Tables["table1"].Rows[index];
            DataRow newDr = this.ds.Tables["table1"].NewRow();
            newDr.ItemArray = dr.ItemArray;
            this.ds.Tables["table1"].Rows.RemoveAt(index);
            this.ds.Tables["table1"].Rows.InsertAt(newDr, index - 1);
            this.bindingSource1.Position = index - 1;
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        bindingSource1.Sort = "";
        int index = this.bindingSource1.Position;

        if (index < this.bindingSource1.Count)
        {
            DataRow dr = (DataRow)this.ds.Tables["table1"].Rows[index];
            DataRow newDr = this.ds.Tables["table1"].NewRow();
            newDr.ItemArray = dr.ItemArray;
            this.ds.Tables["table1"].Rows.RemoveAt(index);
            this.ds.Tables["table1"].Rows.InsertAt(newDr, index + 1);
            this.bindingSource1.Position = index + 1;
        }
    }
} 

1 个答案:

答案 0 :(得分:0)

将DataBind()添加到网格的onSorted和onPagedChanged事件中。