dataGrid dataview editing error after sorting

  • Thread starter Thread starter David Dimmer
  • Start date Start date
D

David Dimmer

I am trying to implement sorting and editing on a datagrid that has it's
source as a DataView. I can sort by selecting the column headers, and I can
edit using. However, If I sort, then edit, the table reverts to the
original 'sorting', and selects the 'wrong' row for editing... I.e. if my
datagrid is as follows:

A
W
E
D

I select the column to sort, it becomes:
A
D
E
W

However, when I select the 'D' row for editing, the table reverts to the
original 'AWED' order, and the 'W' row is selected for editing.

The code below may provide some insight:


//Walkthrough: Using a DataGrid Web Control to Read and Write Data
private void DataGrid1_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
dataView.Sort=e.SortExpression;
DataGrid1.DataBind();
}

private void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}

private void DataGrid1_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex =-1;
DataGrid1.DataBind();
}

private void Page_Load(object sender, System.EventArgs e)
{
sqlConnection = (SqlConnection)Session["sqlConnection"];
sqlDataAdapter = (SqlDataAdapter)Session["sqlDataAdapter"];

// Put user code to initialize the page here
sqlDataAdapter.Fill(dsProjMilestones);
if (!IsPostBack)
{
DataGrid1.DataBind();
}
}


Thanks for any help.
 
You have to provide the same DataView on each round trip. In other
words, save your sort expression, and when you do the "edit", make
sure your DataView is sorted the same.

--------------

And the reply from DataGridGirl.com (Thank you!):

Hi David,
You'll want to store off the SortExpression somewhere if they've
sorted the data, then sort to that order before setting the
EditItemIndex.

Datagrid Girl

--------------

And my co-worker Ryan:

You need to store the sort expression and reset dataView.Sort just
before anytime you call DataBind. This will also allow you to do a
reverse sort, with a little extra coding, when the user clicks the
column a second time.


Ryan Sexton
Senior Software Engineer
 
Back
Top