2011-06-06 58 views
0

我有關聯到的BindingSource以下的DataGridView:上移/下移行不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執行排序操作後行不會移動?

謝謝。 -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

請至少在下次時間刪除所有冗餘空行。 – 2011-06-06 12:58:19

+0

而你的問題是什麼?或者簡短的回答:是的。 – Bobby 2011-06-06 12:59:50

回答

0

添加DataBind()到網格的onSorted和onPagedChanged事件。

+0

winform datagridview沒有DataBind()方法。 – Alessandro 2011-06-06 19:17:21

+0

原諒我。我一定錯過了。 OP是否指定winforms? – MAW74656 2011-06-06 19:21:39

+0

在我的問題上添加了新的部分.. – Alessandro 2011-06-06 19:23:40