Sorting Problem with Viewstate

J

John Smith Jr.

I did my first DataGrid with sorting. Problem I have is viewstate has to be
on for it to work, even though i am passing a dataview and re-binding the
datagrid with it as needed. This means the data is passed for the table,
the viewstate for the page as ussual, then the viewstate var i am using to
pass the dataview.

Here is snipit of what i am doing, if someone could tell me if I am missing
something, that would be great, although everything does work. I think I am
sending way too much traffic than I need to.

private void Page_Load(object sender, System.EventArgs e)

{

if (!IsPostBack)

{

sqlDataAdapter1.Fill(dataSet11);


Session["dataView"] = dataSet11.Tables["Customers"].DefaultView;

ViewState["Sort"] = string.Empty;

ViewState["SortDirection"] = "ASC";

DataGrid1.DataSource = Session["dataView"];

DataGrid1.DataBind();

}



}



private void DataGrid1_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)

{

string sortDirection = "ASC";

DataView dv = (DataView) Session["dataView"];

if (e.SortExpression == ViewState["Sort"].ToString() )

{

if (ViewState["SortDirection"].ToString() == "ASC")

{

sortDirection = "DESC";

}

else

{

sortDirection = "ASC";

}

}


dv.Sort = e.SortExpression + " " + sortDirection;



Session["dataView"] = dv;

ViewState["SortDirection"] = sortDirection;

ViewState["Sort"] = e.SortExpression;

DataGrid1.DataSource = Session["dataView"];

DataGrid1.DataBind();

}
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top