datagridview列排序后,MoveUp/MoveDown行不起作用 [英] MoveUp / MoveDown row doesn't work after datagridview collumn sorting

查看:122
本文介绍了datagridview列排序后,MoveUp/MoveDown行不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个与bindingSource关联的DataGridView:

Hello, I have a DataGridView associated to a bindingSource:

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



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



And I have two functions to move up and down a selected row:

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



The two methods works fine and when I click on button to move the row, it gets correctly moved.

But if I click before on a column in order to sort it (click on Header) and after try to move again a row, the binding source position it''s moved but the row in the datagridview not.
I debug the functions and nothing goes wrong, it seems just a datagridview visualization error.
I tried to expand the sorting on the binding source, in the dataGridView1_Sorted handled event but still doesn''t works.
Why the row is not moved after a sort operation on a datagridview?

thanks.
-Alessandro

推荐答案

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

I made some progress but still there''re some problems after sort by clicking on header grid collumn. I tried to reset the bindingSource1.Sort = ""; in the move rows function, and the row and now the row get moved but the position is wrong!! Here the code so you try by yourself..

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;
           }
       }
   }


这篇关于datagridview列排序后,MoveUp/MoveDown行不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆