aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Sorting Columns in Repeater Control in using C#, VB.NET

Oct 19, 2014

Here I will explain how to implement sorting in repeater control in using c#, with example or how to sort columns in repeater control in using c#,

In previous articles I explained Repeater Control Example in, Display time like facebook/twitter like 1 minute ago, hour ago, use of using statement in c#, make some cells read only in excel sheet and many articles relating to, c#, and JQuery. Now I will explain how to implement sorting in repeater control in using c#, with example.

To implement sorting in repeater control first create new web application and write the following code in aspx page

<html xmlns="">
<head id="Head1" runat="server">
<title>Repeater Sorting Example in</title>
<style type="text/css">
<form id="form1" runat="server">
<asp:Repeater ID="rptUserData" runat="server" OnItemCommand="rptUserData_ItemCommand">
<table style="width:500px; border-collapse: collapse;" border="1" cellpadding="5" cellspacing="0" >
<tr style="background-color: #df5015; height: 30px;"
<asp:LinkButton ID="lnkUserId" runat="server" CommandName="UserId" CssClass="hrefclass">UserId</asp:LinkButton></th>
<asp:LinkButton ID="lnkUserName" runat="server" CommandName="UserName" CssClass="hrefclass">UserName</asp:LinkButton></thalign>
<asp:LinkButton ID="lnkEducation" runat="server" CommandName="Education" CssClass="hrefclass">Education</asp:LinkButton></th>
<tr style="height: 25px;">
<td >
<td >
Now open code behind file and write following code

C# Code

using System;
using System.Data;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            //Set View State Value for Initial Sort Order
            ViewState["Column"] = "UserId";
            ViewState["Sortorder"] = "ASC";
    private void BindRepeater()
        DataTable dt = new DataTable();
        dt.Columns.Add("UserId", typeof(Int32));
        dt.Columns.Add("UserName", typeof(string));
        dt.Columns.Add("Education", typeof(string));
        dt.Rows.Add(1, "Suresh Dasari", "B.Tech");
        dt.Rows.Add(2, "Rohini Dasari", "Msc");
        dt.Rows.Add(3, "Madhav Sai", "MS");
        dt.Rows.Add(4, "Praveen", "B.Tech");
        dt.Rows.Add(6, "Sateesh", "MD");
        dt.Rows.Add(7, "Mahesh Dasari", "B.Tech");
        dt.Rows.Add(8, "Mahendra", "CA");
        DataView dvData = new DataView(dt);
        //Sorting Filter
        dvData.Sort = ViewState["Column"] + " " + ViewState["Sortorder"];
        rptUserData.DataSource = dvData;
    protected void rptUserData_ItemCommand(object source, RepeaterCommandEventArgs e)
        if (e.CommandName == ViewState["Column"].ToString())
            if (ViewState["Sortorder"].ToString() == "ASC")
                ViewState["Sortorder"] = "DESC";
                ViewState["Sortorder"] = "ASC";
            ViewState["Column"] = e.CommandName;
            ViewState["Sortorder"] = "ASC";

Imports System.Data
Imports System.Web.UI.WebControls

Partial Class VbCode
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'Set View State Value for Initial Sort Order
            ViewState("Column") = "UserId"
            ViewState("Sortorder") = "ASC"
        End If
    End Sub
    Private Sub BindRepeater()
        Dim dt As New DataTable()
        dt.Columns.Add("UserId", GetType(Int32))
        dt.Columns.Add("UserName", GetType(String))
        dt.Columns.Add("Education", GetType(String))
        dt.Rows.Add(1, "Suresh Dasari", "B.Tech")
        dt.Rows.Add(2, "Rohini Dasari", "Msc")
        dt.Rows.Add(3, "Madhav Sai", "MS")
        dt.Rows.Add(4, "Praveen", "B.Tech")
        dt.Rows.Add(6, "Sateesh", "MD")
        dt.Rows.Add(7, "Mahesh Dasari", "B.Tech")
        dt.Rows.Add(8, "Mahendra", "CA")
        Dim dvData As New DataView(dt)
        'Sorting Filter
        dvData.Sort = ViewState("Column") + " " + ViewState("Sortorder")
        rptUserData.DataSource = dvData
    End Sub
    Protected Sub rptUserData_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs)
        If e.CommandName = ViewState("Column").ToString() Then
            If ViewState("Sortorder").ToString() = "ASC" Then
                ViewState("Sortorder") = "DESC"
                ViewState("Sortorder") = "ASC"
            End If
            ViewState("Column") = e.CommandName
            ViewState("Sortorder") = "ASC"
        End If
    End Sub
End Class

Sorting Columns in Repeater Control in using C#, VB.NET

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Anonymous said...

Error 1 'rptUserData_ItemCommand' is not a member of 'ASP.default_aspx'. D:\Praveen@Work\WebSite1\Default.aspx 15
i am beginner in dot net i am getting this error please help...i copy pasted ur code

Bhavik Joshi said...

nice article !
but what i need to fix is repeater control Data is there only but i used dynamic data from database so DataView didn't support it .
so please help me in to fix dynamic Repeater Data Sorting by clicking Header same as here .
Thank you in advance !

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.