dataGrid dataview sorting/editing

M

Martin Schmid

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.
 
M

Martin Schmid

Here is an example of what I mean (this is not my code/page... but it
exhibits the same behavior). Note if you sort on a column, then Edit the
row, the sort order reverts, and the wrong item becomes editable. Anyone
have any idea on a fix?

http://www.asplists.com/experiments/bookdata/booksgridevents.aspx

This link was from :

http://www.asplists.com/freebook/learn/gridevents.aspx

Note in the DataSort subroutine: ' Bug if we sort, then Edit Item
Becomes Wrong

Granted, this example is VB, and my code is C#, but it seems to be the same
problem.
 
M

Martin Schmid

I moved my dataView and my dsProjMilestones definitions to the Global.asax
file, and defined session variables for each:

protected void Session_Start(Object sender, EventArgs e)
{
Session["sqlConnection"] = sqlConnection;
Session["sqlDataAdapter"] = sqlDataAdapter;
Session["dataView"] = dataView;
Session["dsProjMilestones"] = dsProjMilestones;
}

I revised my web form.aspx as follows:
protected DataView dataView;
protected ProjContMan.dsProjMilestones dsProjMilestones;
private void Page_Load(object sender, System.EventArgs e)
{
sqlConnection = (SqlConnection)Session["sqlConnection"];
sqlDataAdapter = (SqlDataAdapter)Session["sqlDataAdapter"];
dataView=(DataView)Session["dataView"] ;

dsProjMilestones=(ProjContMan.dsProjMilestones)Session["dsProjMilestones"] ;

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

This has given me the results that I expect, i.e., I can sort, then edit.
Is this the 'correct' way to do this? I invite comments as I am trying to
learn this as quickly as possible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top