Gridview Sorting Using C#




In the Previous Article I Discussed about GridviewPaging

ASP.Net 3.0 provides the sorting feature also in GridView Control. You can sort the records displayed using Gridview control in ascending or descending order retrieved from sql database. You can use C# code to bind the SQL data with GridView control and follow the following simple steps to make your ASP.Net GridView control with sorting enabled. 


First of all drag the GridView control from Data controls menu. It will add the GridView control HTML source code as given above. Now click on GridView control to load the control properties at right side panel.


<asp:GridView id="GridView1" runat="server"></asp:GridView>



To allow the sorting in GridView control select True from the dropdown list of AllowSorting property of Gridview as shown in above image.



This will add AllowSorting="True" in HTML source code of GridView Control.

Next step is to bind the Sorting event for GridView Control. 


To bind the Sorting event of GridView control, double click on the Sorting event in the properties viewer of GridView. This will add the Sorting event in the HTML source code and C# code for Gridview.



<asp:GridView runat="server" ID="gvEmpdetails" DataKeyNames="EmpID" 
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" OnSorting="gvEmpdetails_OnSorting">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox runat="server" ID="chkhead" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EmpID" HeaderText="EmpID" SortExpression="EmpID" />
<asp:BoundField DataField="EmpName" HeaderText="EmpName" SortExpression="EmpName" />
<asp:BoundField DataField="EmpSal" HeaderText="EmpSal" SortExpression="EmpSal" />
<asp:BoundField DataField="EmpBranch" HeaderText="EmpBranch" SortExpression="EmpBranch" />
<asp:BoundField DataField="EmpCode" HeaderText="EmpCode" SortExpression="EmpCode" />
</Columns>
</asp:GridView>

In the Code Behind Page OnSorting Event As Follows:


    
    //Page Load....

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["sortOrder"] = "";
            bindGridView("", "");
        }
    }

Binding Data To Gridview Control and Sorting Using DataView

    
   // binding Gridview Data From DataBase...
    public void bindGridView(string sortExp, string sortDir)
    {       
        SqlConnection con =new SqlConnection("Data Source=TAPA-PC;user Id=sa;password=123;DataBase=Customer");
        SqlDataAdapter mySqlAdapter = new SqlDataAdapter("select * from Employee", con);
        DataSet myDataSet = new DataSet();
        mySqlAdapter.Fill(myDataSet);
        DataView myDataView = new DataView();
        myDataView = myDataSet.Tables[0].DefaultView;
        if (sortExp != string.Empty)
        {
            myDataView.Sort = string.Format("{0} {1}", sortExp, sortDir);
        }
        gvEmpdetails.DataSource = myDataView;
        gvEmpdetails.DataBind();

    }


In the Code Behind  Sorting Event as follows:

     //Sorting event...
    protected void gvEmpdetails_OnSorting(object sender, GridViewSortEventArgs e)
    {
        bindGridView(e.SortExpression, sortOrder);       
    }



Sort Order Entity  as follows :

   
    //Sort Order Entity....
    public string sortOrder
    {
        get
        {
            if (ViewState["sortOrder"].ToString() == "desc")
            {
                ViewState["sortOrder"] = "asc";
            }
            else
            {
                ViewState["sortOrder"] = "desc";
            }

            return ViewState["sortOrder"].ToString();
        }
        set
        {
            ViewState["sortOrder"] = value;
        }
    }



Screen Shots:
Data Bind To Gridview from Database
 Click on the EmpName Header  To Sort the EmpName Column:
  Click on the EmpSal Header To Sort the EmpSal Column:

1 Responses to “Gridview Sorting Using C#”

Anonymous said...
27 February 2014 at 18:32

Sorting DataGridView in C#


Post a Comment

Labels

Topics