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

C# datagridview列排序后,MoveUp/MoveDown行不起作用,c#,data-binding,sorting,datagridview,C#,Data Binding,Sorting,Datagridview,我将以下DataGridView与bindingSource关联: bindingSource1.DataSource = dataset1; bindingSource1.DataMember = "table1"; dataNavigator1.DataSource = bindingSource1; dataGridView1.DataSource = bindingSource1; 我有两个函数可以在选定的行上下移动: private void buttonUp_Click(objec

我将以下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;
    }
}
这两种方法都很好,当我点击按钮移动行时,它会被正确地移动

但如果我在列之前单击以对其进行排序,请单击标题,然后尝试再次移动一行,绑定源位置将被移动,但datagridview中的行不会被移动。 我调试了这些函数,没有出现任何问题,这似乎只是一个datagridview可视化错误。 我尝试在dataGridView1_Sorted handled事件中扩展绑定源上的排序,但仍然不起作用。 为什么在datagridview上执行排序操作后不移动该行

谢谢。 -亚历山德罗

我取得了一些进展,但通过单击标题网格列进行排序后仍然存在一些问题。我试图重置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;
        }
    }
} 

将数据绑定添加到网格的onSorted和onPagedChanged事件中。

下次请自己花至少一点时间删除所有多余的空行。您的问题是什么?或者简单回答:是的。winform datagridview没有数据绑定方法。请原谅。我一定错过了。OP是否指定winforms?