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

    How to Perform Sorting in asp.net

    hi friends,
    how to perform sorting in asp.net. and please check my below snopshot.
    Thanks and Regards
  • #766248
    Hi,
    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">
    <-Columns>
    <-asp:TemplateField HeaderText="Student ID" SortExpression="StdId">
    <-ItemTemplate>
    <-asp:Label ID="lblStudId" runat="server" Text='<%#Eval("StdId")%>' />
    <-/ItemTemplate>
    <-/asp:TemplateField>
    <-/Columns>
    <-/asp:GridView>


    .CS Code:

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

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

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

    For adding that up/down arrow like images to a header, use code given in this URL:
    http://www.codeproject.com/Tips/663532/How-to-Perform-Sorting-in-Gridview-in-ASP-NET

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

  • #766251
    Hi,

    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.

    Ex:

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

    rpt.DataSource=ds;
    rpt.DataBind();


    Hope this will helpful to you...

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

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #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
    http://www.aspdotnet-suresh.com/2014/10/csharp-sorting-columns-in-repeater-control-in-aspnet.html

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

  • #766259
    Hi
    try this code

    Client Side



    <asp:GridView ID="gvDetails" runat="server" AllowSorting="true"
    AutoGenerateColumns="False" >
    <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>



    Server Side Code


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


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

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

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

  • #766271
    Hi,
    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