Reputation: 267
I've tried for so many times to find a way to sort my gridview but I couldn't find a way to do it.
Table Columns:
Name | Age | Old device name
============================
John | 22 | KRT 20
Jon | 21 | KSR 12
Jco | 22 |
I would like to know how can I make a drop list , to choose in the gridview to sort all the rows by Old device name , so I should see in the gridview only the rows that contains the old device name.
I'm using a SqlDataSource and GridView in ASP.NET.
Upvotes: 2
Views: 1992
Reputation: 11433
EDIT: I've updated the code based on your comments.
One way to do this is to set your DropDownList to AutoPostBack="true"
, and handle the SelectedIndexChanged
event. In this example, the items in your DropDownList should match the column names of your GridView. I've used "ColumnA", "ColumnB", and "ColumnC"
<asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server"
OnSelectedIndexChanged="myDDL_SelectedIndexChanged">
<asp:ListItem>ColumnA</asp:ListItem>
<asp:ListItem>ColumnB</asp:ListItem>
<asp:ListItem>ColumnC</asp:ListItem>
</asp:DropDownList>
Then, in code behind, you can set the "SortExpression" of your just call the sort function based on your dropw down's selected value:GridView
and databind it
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
gridView1.Sort(DropDownList1.SelectedValue, SortDirection.Ascending);
}
You could replace the SortDirection.Ascending
with SortDirection.Descending
.
Note: This answer assumes that the values in your drop down are the names of columns / fields in your datasource.
Upvotes: 2