Sorting of Gridview using LinqDataSource at runtime
-
Hi all, I have dropdownlist, in that items('All','Confirmed','NotConfirmed') . I have 3 LinqDataSource Controls for each of dropdownlist items. Basing on selected item in dropdownlist, i am assigning LinqDataSource to Gridview. How to perform soting at runtime using LinqDataSource ? In Gridview_Sorting(..)Event i wrote like this: { if(e.SortDirection == SortDirection.Ascending) { if(ddl_items.SelectedItem.Text == "All") { LinqDataSorce_All.OrderBy = e.SortExpression + " "+SortDirection.Descending; gview.DataSourceID = "LinqDataSorce_All"; } else if(ddl_items.SelectedItem.Text == "Confirmed") { LinqDataSorce_Confirmed.OrderBy = e.SortExpression + " "+SortDirection.Descending; gview.DataSourceID = "LinqDataSorce_Confirmed"; } else if(ddl_items.SelectedItem.Text == "NotConfirmed") { LinqDataSorce_NotConfirmed.OrderBy = e.SortExpression + " "+SortDirection.Descending; gview.DataSourceID = "LinqDataSorce_NotConfirmed"; } } //end of if else { if(ddl_items.SelectedItem.Text == "All") { LinqDataSorce_All.OrderBy = e.SortExpression + " "+SortDirection.Ascending; gview.DataSourceID = "LinqDataSorce_All"; } else if(ddl_items.SelectedItem.Text == "Confirmed") { LinqDataSorce_Confirmed.OrderBy = e.SortExpression + " "+SortDirection.Ascending; gview.DataSourceID = "LinqDataSorce_Confirmed"; } else if(ddl_items.SelectedItem.Text == "NotConfirmed") { LinqDataSorce_NotConfirmed.OrderBy = e.SortExpression + " "+SortDirection.Ascending; gview.DataSourceID = "LinqDataSorce_NotConfirmed"; } }//end of else gview.DataBind(); }//end of event The Sorting is Working fine, but the problem is when i Click on Next page to gridview the soting order not remains the same. The sorted order was changed. Plz suggest me how can i over come from this? Thanks in advance.
-
Hi all, I have dropdownlist, in that items('All','Confirmed','NotConfirmed') . I have 3 LinqDataSource Controls for each of dropdownlist items. Basing on selected item in dropdownlist, i am assigning LinqDataSource to Gridview. How to perform soting at runtime using LinqDataSource ? In Gridview_Sorting(..)Event i wrote like this: { if(e.SortDirection == SortDirection.Ascending) { if(ddl_items.SelectedItem.Text == "All") { LinqDataSorce_All.OrderBy = e.SortExpression + " "+SortDirection.Descending; gview.DataSourceID = "LinqDataSorce_All"; } else if(ddl_items.SelectedItem.Text == "Confirmed") { LinqDataSorce_Confirmed.OrderBy = e.SortExpression + " "+SortDirection.Descending; gview.DataSourceID = "LinqDataSorce_Confirmed"; } else if(ddl_items.SelectedItem.Text == "NotConfirmed") { LinqDataSorce_NotConfirmed.OrderBy = e.SortExpression + " "+SortDirection.Descending; gview.DataSourceID = "LinqDataSorce_NotConfirmed"; } } //end of if else { if(ddl_items.SelectedItem.Text == "All") { LinqDataSorce_All.OrderBy = e.SortExpression + " "+SortDirection.Ascending; gview.DataSourceID = "LinqDataSorce_All"; } else if(ddl_items.SelectedItem.Text == "Confirmed") { LinqDataSorce_Confirmed.OrderBy = e.SortExpression + " "+SortDirection.Ascending; gview.DataSourceID = "LinqDataSorce_Confirmed"; } else if(ddl_items.SelectedItem.Text == "NotConfirmed") { LinqDataSorce_NotConfirmed.OrderBy = e.SortExpression + " "+SortDirection.Ascending; gview.DataSourceID = "LinqDataSorce_NotConfirmed"; } }//end of else gview.DataBind(); }//end of event The Sorting is Working fine, but the problem is when i Click on Next page to gridview the soting order not remains the same. The sorted order was changed. Plz suggest me how can i over come from this? Thanks in advance.
You're sorting in the wrong place, as the Gridview sorting even only fires when you *change* the sort. This is not remembered on a postback. Gridview can handle the sorting itself, provided you specify the
SortExpression
value. It sounds like the three LinqDataSources do different selects based on the same table, and your dropdown list specifies the filter. There is an easier and simpler way: 1) replace the three LinqDataSources with just one LinqDataSource, which does the select without a filter on the table. 2) handle the LinqDataSource_Selecting event, and apply the filter to the query:void LinqDataSource_Selecting(.....)
{
var db = new MyDataContext();
// normally I would move this to the Table class as
// a static method, this is for demo purposes
switch (this.DropdownList1.SelectedValue)
{
case "All":
// no filter
e.Result = from x in db.Table
select x;case "Confirmed":
// no filter
e.Result = from x in db.Table
where x.Status=somevalue
select x;case "Unconfirmed":
// unconfirmed filter
e.Result = from x in db.Table
where x.Status=someothervalue
select x;default:
// unknown
throw new ApplicationException("unknown filter");
}
}Note that I didn't sort here, LinqDataSource will apply the sort.
'Howard
-
You're sorting in the wrong place, as the Gridview sorting even only fires when you *change* the sort. This is not remembered on a postback. Gridview can handle the sorting itself, provided you specify the
SortExpression
value. It sounds like the three LinqDataSources do different selects based on the same table, and your dropdown list specifies the filter. There is an easier and simpler way: 1) replace the three LinqDataSources with just one LinqDataSource, which does the select without a filter on the table. 2) handle the LinqDataSource_Selecting event, and apply the filter to the query:void LinqDataSource_Selecting(.....)
{
var db = new MyDataContext();
// normally I would move this to the Table class as
// a static method, this is for demo purposes
switch (this.DropdownList1.SelectedValue)
{
case "All":
// no filter
e.Result = from x in db.Table
select x;case "Confirmed":
// no filter
e.Result = from x in db.Table
where x.Status=somevalue
select x;case "Unconfirmed":
// unconfirmed filter
e.Result = from x in db.Table
where x.Status=someothervalue
select x;default:
// unknown
throw new ApplicationException("unknown filter");
}
}Note that I didn't sort here, LinqDataSource will apply the sort.
'Howard
Thanks Richards, i will try that and let u know the result.