Sorting DataList in asp.net

Hello Folks, I was wondered while searching for content that how to do sorting in datalist, but didn’t get any useful content. Finally i end up on searching and started thinking that how i will do it than idea strikes my mind and succeed in that. So i want to share this with you guys, hope it will help you. I will explain it with example:

Following is Sample.aspx code.

<div>
    <asp:DataList ID="dtlist" runat="server" SkinID="CM" HeaderStyle-CssClass="GridViewHeaderStyle" ItemStyle-CssClass="" Width="85%" border="1" Visible="true" 
        AllowPaging="true" PageSize="10" OnPageIndexChanging="" >
        <HeaderTemplate>
            <table width="100%">
                <tr>
                    <td width="20%" valign="top"><b><asp:LinkButton ID="lnkCreatedby" runat="server" OnClick="lbtnDateSort_Click" CommandName="CreatorName">Create By</asp:LinkButton></b></td>
                    <td width="20%" valign="top"><b><asp:LinkButton ID="lbtnDateSort" runat="server" OnClick="lbtnDateSort_Click" CommandName="created_dt">Date</asp:LinkButton></b></td>
                </tr>
            </table>
        </HeaderTemplate>
        <ItemTemplate>
            <table width="100%">
                <tr>
                    <td width="20%" align="center" valign="top">
                        <asp:Label ID="lblCreator" runat="server"><span><%#Eval("CreatorName") %></span></asp:Label></td>
                    <td width="20%" align="center" valign="top">
                        <asp:Label ID="lblcreateddt" runat="server"><span><%#Eval("created_dt") %></span></asp:Label></td>
                </tr>
            </table>
        </ItemTemplate>
    </asp:DataList>
</div>

First i have created datalist in which i have bind data. You can use more than two columns for your datalist and don’t forget to write correct column name (;-)). And give same column name to CommandName because it will be used in sorting data. OnClick event name will be the same name for all linkbutton to make it easy. Otherwise you can write different events but it will increase your coding efforts. So, better use one method and just pass different CommandName which will differentiate your execution.

Following is Sample.aspx.cs code.

  protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                ViewState["SortOrder"] = 0;
                // Your Code goes here;
            }
        }
        catch
        {}
    }

ViewState[“Sortorder”] is assigned on Page_Load event for defaulting ascending order. It will keep on changing as shown in below code.

 protected void lbtnDateSort_Click(object sender, EventArgs e)
    {
        LinkButton lnlbtn = (LinkButton)sender;

        DataTable dtdlNotes = new DataTable();
        dtdlNotes = (DataTable)ViewState["Notes"];
        if (Convert.ToInt32(ViewState["Sortorder"]) == 0)
        {
            ViewState["Sortorder"] = 1;
            dtdlNotes.DefaultView.Sort = lnlbtn.CommandName.ToString() + " ASC";
        }
        else if (Convert.ToInt32(ViewState["Sortorder"]) == 1)
        {
            ViewState["Sortorder"] = 0;
            dtdlNotes.DefaultView.Sort = lnlbtn.CommandName.ToString() + " DESC";
        }
        dtdlNotes.AcceptChanges();
        dlNotes.DataSource = dtdlNotes;
        dlNotes.DataBind();
    }

In code behind add event lbtnDateSort_Click which used to sort data. Here ViewState[“Notes”] is used in which all data stored while binding data. Using CommandName you can easily sort that column either in ascending or descending order.

Hope it will work for you. Don’t forget to share your views. Keep Smiling 🙂

Leave a Reply