【问题标题】:MoveUp / MoveDown row doesn't work after datagridview column sorting在 datagridview 列排序后,MoveUp / MoveDown 行不起作用
【发布时间】:2011-09-09 06:53:34
【问题描述】:

我有以下与 bindingSource 关联的 DataGridView:

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 处理的事件中扩展绑定源的排序,但仍然不起作用。 为什么对 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;
        }
    }
} 

【问题讨论】:

  • 下次请至少花点时间自己删除所有多余的空行。
  • 你的问题是什么?或者简短的回答:Yes.

标签: c# data-binding sorting datagridview


【解决方案1】:

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

【讨论】:

  • winform datagridview 没有 DataBind() 方法。
  • 原谅我。我一定错过了。 OP是否指定了winforms?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-14
  • 2010-10-12
  • 2021-02-25
  • 1970-01-01
  • 1970-01-01
  • 2015-09-21
  • 1970-01-01
相关资源
最近更新 更多