开发者

MoveUp / MoveDown row doesn't work after datagridview column sorting

I have the following 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);
     开发者_C百科   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

I made some progress but still there're some problems after sort by clicking on header grid column. 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;
        }
    }
} 


Add DataBind() to the grid's onSorted and onPagedChanged events.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜