/* ------------------------ My Meta Content Here SEO ------------------------ */

Pages

Main Menu

Wednesday, August 22, 2012

ASP.NET Sorting in GridView By Columns Header In Asp.Net Ascending Descending


http://csharpdotnetfreak.blogspot.com/2012/06/sorting-gridview-columns-headers-aspnet.html

Sorting GridView By Columns Header In Asp.Net Ascending Descending

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 

<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

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            gvDetails.DataSource = BindGridView();
            gvDetails.DataBind();
        }
    }

    private DataTable BindGridView()
    {
        DataTable dtGrid = new DataTable();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        string strSelect = "SELECT FirstName,LastName,Location FROM Details";
        SqlCommand cmd = new SqlCommand(strSelect, con);
        SqlDataAdapter dAdapter = new SqlDataAdapter(cmd);
        dAdapter.Fill(dtGrid);
        return dtGrid;
    }
VB.NET CODE

Protected Sub Page_Load(sender As Object, e As EventArgs)
 If Not Page.IsPostBack Then
  gvDetails.DataSource = BindGridView()
  gvDetails.DataBind()
 End If
End Sub

Private Function BindGridView() As DataTable
 Dim dtGrid As New DataTable()
 Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
 Dim strSelect As String = "SELECT FirstName,LastName,Location FROM Details"
 Dim cmd As New SqlCommand(strSelect, con)
 Dim dAdapter As New SqlDataAdapter(cmd)
 dAdapter.Fill(dtGrid)
 Return dtGrid
End Function
Create Public Property of SortDirection type and store direction in ViewState.
C# CODE
public SortDirection dir
    {
        get
        {
            if (ViewState["dirState"] == null)
            {
                ViewState["dirState"] = SortDirection.Ascending;
            }
            return (SortDirection)ViewState["dirState"];
        }
        set
        {
            ViewState["dirState"] = value;
        }
    }
VB.NET CODE
Public Property dir() As SortDirection
 Get
  If ViewState("dirState") Is Nothing Then
   ViewState("dirState") = SortDirection.Ascending
  End If
  Return DirectCast(ViewState("dirState"), SortDirection)
 End Get
 Set
  ViewState("dirState") = value
 End Set
End Property
Check Gridview's current direction from ViewState and set new sort direction in Sorting Event.
C# CODE
protected void gvDetails_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortingDirection = string.Empty;
        if (dir == SortDirection.Ascending)
        {
            dir = SortDirection.Descending;
            sortingDirection = "Desc";
        }
        else
        {
            dir = SortDirection.Ascending;
            sortingDirection = "Asc";
        }
        
        DataView sortedView = new DataView(BindGridView());
        sortedView.Sort = e.SortExpression + " " + sortingDirection;
        gvDetails.DataSource = sortedView;
        gvDetails.DataBind();
    }
VB.NET CODE
Protected Sub gvDetails_Sorting(sender As Object, e As GridViewSortEventArgs)
 Dim sortingDirection As String = String.Empty
 If dir = SortDirection.Ascending Then
  dir = SortDirection.Descending
  sortingDirection = "Desc"
 Else
  dir = SortDirection.Ascending
  sortingDirection = "Asc"
 End If

 Dim sortedView As New DataView(BindGridView())
 sortedView.Sort = Convert.ToString(e.SortExpression) & " " & sortingDirection
 gvDetails.DataSource = sortedView
 gvDetails.DataBind()
End Sub

No comments:

Post a Comment

My Blog List

  • काश - काश मुझे भी पीने की आदत होती,मैं कब का मुर्दा हो गया होता। छुटकारा मिलता आज के आतंकवाद से, किसी संतान भूमि में सो गया होता। मेरा एतबार कौन करेगा, मैंने मुर...
    3 months ago
  • काश - काश मुझे भी पीने की आदत होती,मैं कब का मुर्दा हो गया होता। छुटकारा मिलता आज के आतंकवाद से, किसी शमशान भूमि में सो गया होता। मेरा एतबार कौन करेगा, मैंने मुर...
    3 months ago
  • Kumaon University Nainital B.Ed entrance exam test result 2012 - कुमाऊँ विश्वविधालय, नैनीताल (उत्तराखण्ड)
    10 years ago