Reputation: 691
Here is my GV.
<asp:GridView ID="Grid1" runat="server" AutoGenerateColumns="false"
AllowPaging="True" OnPageIndexChanging="Grid1_PageIndexChanging">
<Columns>
<asp:BoundField DataField="One" HeaderText="One" />
<asp:BoundField DataField="Two" HeaderText="Two" />
<asp:BoundField DataField="Three" HeaderText="Three" />
</Columns>
</asp:GridView>
I'm populating the GV with a Stored Procedure.
table = PublicClass.Sql_GetTable("usp_GetReportGridView", "NCIU");
Grid1.DataSource = table;
Grid1.DataBind();
How can I sort using the column headers?
Upvotes: 0
Views: 144
Reputation:
First you need to enable AllowSorting property to be true. When enabled, the grid renders LinkButton controls in the header for each column. When the button is clicked, the grid's SortCommand event is thrown. It's up to the you to handle this event in the code. Because DataGrid always displays the data in the same order it occurs in the data source, the typical logic sorts the data source, and then rebinds the data to the grid.look at code below:
//AllowSorting="True"
//OnSorting="GridView2_Sorting"
//SortExpression="name"
protected void GridView2_Sorting(object sender, GridViewSortEventArgs e)
{
//to check whether to display in ascending order or descending order
if (e.SortExpression.Trim() == this.SortField)
this.SortDirection = (this.SortDirection == "D" ? "A" : "D");
else
this.SortDirection = "A";
this.SortField = e.SortExpression;
TempTable();
}
Upvotes: 2