This is example of how to enable Sorting In GridView By Clicking on Columns Or Headers In Ascending Or Descending Direction In Asp.Net Using C# And VB
If you have Populated GridView With ObjectDataSource or SqlDataSOurce to Insert Update Edit Delete record , then You just have toset AllowSorting property to True andSortExpression property of columns to respective field from database to sort.
Setting these properties changes column names to hyperlink and No coding is required.
Write html source as mentioned below
IF SQLCONNECTION AND SQLCOMMAND IS USED IN CODE BEHIND
If data is populated from database using code behind, then to sortwe need to write custom codeto save direction (Ascending or Descending) in ViewState and writecode in Sorting Event of Grid.
Populate and load GridView on Page_Load Event
BindGridView Method fetches data from database and return it as DataTable.
C# CODE
VB.NET CODE
Create Public Property of SortDirection type and store direction in ViewState.
C# CODE
VB.NET CODE
Check Gridview's current direction from ViewState and set new sort direction in Sorting Event.
C# CODE
VB.NET CODE
If you have Populated GridView With ObjectDataSource or SqlDataSOurce to Insert Update Edit Delete record , then You just have toset AllowSorting property to True andSortExpression property of columns to respective field from database to sort.
Setting these properties changes column names to hyperlink and No coding is required.
Write html source as mentioned below
<asp:GridView ID="gvDetails" runat="server"
AutoGenerateColumns="False"
onsorting="gvDetails_Sorting">
<Columns>
<asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
<ItemTemplate>
<asp:Label ID="lblFname" runat="server" Text='<%#Eval("FirstName")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
<ItemTemplate>
<asp:Label ID="lblLname" runat="server" Text='<%#Eval("LastName")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" SortExpression="Location">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%#Eval("Location")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
IF SQLCONNECTION AND SQLCOMMAND IS USED IN CODE BEHIND
If data is populated from database using code behind, then to sortwe need to write custom codeto save direction (Ascending or Descending) in ViewState and writecode in Sorting Event of Grid.
Populate and load GridView on Page_Load Event
BindGridView Method fetches data from database and return it as DataTable.
C# CODE
01protected void Page_Load(object sender, EventArgs e)02 {03 if (!Page.IsPostBack)04 {05 gvDetails.DataSource = BindGridView();06 gvDetails.DataBind();07 }08 }09 10 private DataTable BindGridView()11 {12 DataTable dtGrid = new DataTable();13 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);14 string strSelect = "SELECT FirstName,LastName,Location FROM Details";15 SqlCommand cmd = new SqlCommand(strSelect, con);16 SqlDataAdapter dAdapter = new SqlDataAdapter(cmd);17 dAdapter.Fill(dtGrid);18 return dtGrid;19 }VB.NET CODE
01Protected Sub Page_Load(sender As Object, e As EventArgs)02 If Not Page.IsPostBack Then03 gvDetails.DataSource = BindGridView()04 gvDetails.DataBind()05 End If06End Sub07 08Private Function BindGridView() As DataTable09 Dim dtGrid As New DataTable()10 Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)11 Dim strSelect As String = "SELECT FirstName,LastName,Location FROM Details"12 Dim cmd As New SqlCommand(strSelect, con)13 Dim dAdapter As New SqlDataAdapter(cmd)14 dAdapter.Fill(dtGrid)15 Return dtGrid16End FunctionCreate Public Property of SortDirection type and store direction in ViewState.
C# CODE
01public SortDirection dir02 {03 get04 {05 if (ViewState["dirState"] == null)06 {07 ViewState["dirState"] = SortDirection.Ascending;08 }09 return (SortDirection)ViewState["dirState"];10 }11 set12 {13 ViewState["dirState"] = value;14 }15 }VB.NET CODE
01Public Property dir() As SortDirection02 Get03 If ViewState("dirState") Is Nothing Then04 ViewState("dirState") = SortDirection.Ascending05 End If06 Return DirectCast(ViewState("dirState"), SortDirection)07 End Get08 Set09 ViewState("dirState") = value10 End Set11End PropertyCheck Gridview's current direction from ViewState and set new sort direction in Sorting Event.
C# CODE
01protected void gvDetails_Sorting(object sender, GridViewSortEventArgs e)02 {03 string sortingDirection = string.Empty;04 if (dir == SortDirection.Ascending)05 {06 dir = SortDirection.Descending;07 sortingDirection = "Desc";08 }09 else10 {11 dir = SortDirection.Ascending;12 sortingDirection = "Asc";13 }14 15 DataView sortedView = new DataView(BindGridView());16 sortedView.Sort = e.SortExpression + " " + sortingDirection;17 gvDetails.DataSource = sortedView;18 gvDetails.DataBind();19 }VB.NET CODE
01Protected Sub gvDetails_Sorting(sender As Object, e As GridViewSortEventArgs)02 Dim sortingDirection As String = String.Empty03 If dir = SortDirection.Ascending Then04 dir = SortDirection.Descending05 sortingDirection = "Desc"06 Else07 dir = SortDirection.Ascending08 sortingDirection = "Asc"09 End If10 11 Dim sortedView As New DataView(BindGridView())12 sortedView.Sort = Convert.ToString(e.SortExpression) & " " & sortingDirection13 gvDetails.DataSource = sortedView14 gvDetails.DataBind()15End Sub

0 comments:
Post a Comment