Reputation: 14787
I have a data grid that is bound to a generic collection. In the Page_Load event, I check for !this.IsPostback
and call DataBind on the grid accordingly.
Then if I try to implement sorting by specifying the uniquename and sortexpression, it expects me to call DataBind even if the page is a postback.
How is this situation normally tackled? Calling unconditional DataBind in Page_Load does not seem like a good idea.
Upvotes: 1
Views: 246
Reputation: 295
it is always needed to be rebound if you change the sort as the control will need to be repopulated and not changed. It has been awhile since i have done any asp.net but i'm pretty sure the data is bound on every post back whether you call databind or not and the only reason you don't have to do this manually is because of the viewstate.
Upvotes: 0
Reputation: 16144
Call your databind code at the SortCommand Event:
void DataGrid1_SortCommand(Object sender, DataGridSortCommandEventArgs e)
{
// Retrieve the data source.
DataTable dt = YOURDATA;
// Create a DataView from the DataTable.
DataView dv = new DataView(dt);
// The DataView provides an easy way to sort. Simply set the
// Sort property with the name of the field to sort by.
dv.Sort = e.SortExpression;
// Rebind the data source and specify that it should be sorted
// by the field specified in the SortExpression property.
DataGrid1.DataSource = dv;
DataGrid1.DataBind();
}
Upvotes: 3