SgDotNet
Singapore Professional .NET User Group -For Cool Developers

Moving rows up and down in DataGrid

rated by 0 users
This post has 1 Reply | 1 Follower

Top 10 Contributor
Posts 2,284
icelava Posted: 08-17-2006 4:57 PM
We have a table of data that includes a column of sort numbers, by binding the DataGrid to a DataView with Sort considering that column, the entries are displayed in their usual sorted order. There is a requirement to have two buttons to allow a selected entry/row to swap its order (thus sort number) with the row above or below.

I was playing around with the following code (example of Up click)

--------------------------------------------------
private void btnUp_Click(object sender, System.EventArgs e)
{
    int selectedIndex = this.dataGrid1.CurrentRowIndex;           

    if (selectedIndex == 0)
    {
        return;
    }

    // sortedView = DataView
    DataRowView lowerRow = this.sortedView[selectedIndex - 1];
    DataRowView upperRow = this.sortedView[selectedIndex];

    int lowerOrder = Convert.ToInt32(lowerRow["SortOrder"]);
    int upperOrder = Convert.ToInt32(upperRow["SortOrder"]);
    lowerRow["SortOrder"] = upperOrder;
    upperRow["SortOrder"] = lowerOrder;

    this.dataGrid1.Refresh();
       
}
--------------------------------------------------

In principle this works, but there are occasions when the DataGrid will not re-sort the entries properly.

1. The SortOrder number for the rows may change but the rows themselves remain in their old positions.
2. Only one row's sortOrder number changes, the other remains (e.g. both appear 10, 10 when it ought to be 5, 10)

I have tried dataGrid.Refresh() or dataGrid.Invalidate() and even gotten the underlying DataView.Sort to be reset to no avail. How can I get the DataGrid to properly update the new sort changes? thanks

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Top 10 Contributor
Posts 2,284
Ok a colleague has offered a workable suggestion: simply altering the values to the DataRowViews is not enough. They additionally need to be explicity marked with as end-of-edit

lowerRow.EndEdit();
upperRow.EndEdit();

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Page 1 of 1 (2 items) | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems