You must Sign In to post a response.
  • Category: ASP.NET

    How to Perform Sorting in

    hi friends,
    how to perform sorting in and please check my below snopshot.
    Thanks and Regards
  • #766248
    It seems a gridview with sortable column headers. Try this code:

    .ASPX Code:

    <-asp:GridView ID="gvDemo" runat="server" AllowSorting="true" AutoGenerateColumns="false" OnSorting="gvDemo_Sorting">
    <-asp:TemplateField HeaderText="Student ID" SortExpression="StdId">
    <-asp:Label ID="lblStudId" runat="server" Text='<%#Eval("StdId")%>' />

    .CS Code:

    string _szSortType = "Asc";//... Global variable

    protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)
    gvDemo.DataSource = DT;//... DT=Your DataSource/DataTable
    _szSortType = "Asc";

    protected void gvDemo_Sorting(object sender, GridViewSortEventArgs e)
    gvDemo.DataSource = DT;//... DT=Your DataSource/DataTable
    if (_szSortType == "Asc")
    _szSortType ="Desc";
    sortedView.Sort = e.SortExpression + " " + "Desc";
    _szSortType ="Asc";
    sortedView.Sort = e.SortExpression + " " + "Asc";
    DataView dvSorted = new DataView(DT); //... DT=Your DataSource/DataTable
    gvDemo.DataSource = dvSorted;

    For adding that up/down arrow like images to a header, use code given in this URL:

  • #766249
    Thank you reponding,
    i'am using repeater and sorting buttons are outside the repeater

  • #766251

    If the sort buttons are outside the repeater then pass the sorting fields as input parameters while fetching data from database and bind the data to repeater.


    DataSet ds=//pass sorting columns as i/p parameter and fetch data from database.


    Hope this will helpful to you...

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • #766257
    You can sort columns in repeater controls, first bind dataset to repeater control, then set View State Value for Initial Sort Order and as you want to sort according to column, 'DataView' class has sort method in that you need to bind a datatable with your values and then assign that datatable object to dataView, finally you need to just use 'Sort ' property of DataView to sort your repeater
    see below link for more details

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766259
    try this code

    Client Side

    <asp:GridView ID="gvDetails" runat="server" AllowSorting="true"
    AutoGenerateColumns="False" >
    <asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
    <asp:Label ID="lblFname" runat="server" Text='<%#Eval("FirstName")%>'/>
    <asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
    <asp:Label ID="lblLname" runat="server" Text='<%#Eval("LastName")%>'/>
    <asp:TemplateField HeaderText="Location" SortExpression="Location">
    <asp:Label ID="lblLocation" runat="server" Text='<%#Eval("Location")%>'/>

    Server Side Code

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

    In your Gridview Property AllowSorting="true" Must be Included.

    Name : Dotnet Developer-2015
    Email Id :

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766271
    If your sorting buttons are outside repeater control then you have to rebind it with firing new script on database as:
    SqlDataSource1.SelectCommand = "SELECT * FROM Table ORDER BY ColumnName";
    Place this repeater and those buttons inside update panel so that it will help in case of post back.

  • Sign In to post your comments