Click here to Skip to main content
15,888,209 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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;
    }
}


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
Posted

1 solution

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;
           }
       }
   }
 
Share this answer
 
Comments
Orcun Iyigun 6-Jun-11 16:36pm    
Next time instead of writing a solution to the question please use"Improve question" to make changes on your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900