SIGN UP MEMBER LOGIN:    
ARTICLE

Sorting in Repeater Control in a simple way using Linq

Posted by Madhu K Articles | LINQ with C# November 12, 2010
This tutorial will explain how to implement sorting in repeater control using Linq and this is a simple way of sorting within the repeater control.
Reader Level:
Download Files:
 

This tutorial will explain how to implement sorting in repeater control using Linq and this is a simple way of sorting within the repeater control.

Source code for aspx page:

<table border="1" width="60%">
    <tr style="background-color:#669acc;">
    <td>ID &nbsp;
<asp:LinkButton ID="LinkButton3" runat="server" onclick="LinkButton3_Click">
      <img src="arrowdown.GIF" alt="descending" />
</asp:LinkButton>
    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">
 <img src="arrowup.GIF" alt="ascending" /></asp:LinkButton>
</td>
     <td>Name &nbsp;</td>
     <td>Country &nbsp;</td>
     </tr>
     <asp:Repeater ID="Repeater1" runat="server">

     <ItemTemplate>
     <tr style="background-color:#f2f7fd;">
     <td >
         <%# Eval("sortid") %></td>
     <td><%# Eval("Name") %></td>
     <td><%# Eval("Country") %></td></tr>
     </ItemTemplate>
    </asp:Repeater>
    </table>


Code behind looks like:

connection cnn = new connection();
private void BindRepeater()
    {
        using (var db = cnn.ConDB())
        {
            var select = from table in db.SortTables
                         select table;
            Repeater1.DataSource = select;
            Repeater1.DataBind();
        }
    }

Above method is used to bind the repeater. If you call the BindRepeater() the output looks like

image1.gif

Sorting is done while binding the datasource to the repeater. You can sort the columns based on which column you want. Here I used ID column to sort.

private void sortDescending()
    {
        using (var db = cnn.ConDB())
        {
            var select = from sort in db.SortTables
                         select sort;
            Repeater1.DataSource = select.OrderByDescending(item => item.SortID);
            Repeater1.DataBind();
            LinkButton3.Visible = false;
            LinkButton1.Visible = true;
        }
    }

select.OrderByDescending(item => item.SortID); where sort ID is the ID column.
Once you call the sortDescending(), the output looks as

image2.gif

You can observe the rows are changed according to the descending order of the ID column.

Similarly you can also sort in ascending order using below code

private void sortAscending()
    {
        using (var db = cnn.ConDB())
        {
            var select = from sort in db.SortTables
                         select sort;
            Repeater1.DataSource = select.OrderBy(item => item.SortID);
//Sort using Name select.OrderBy(item => item.Name);
//Similarly using Country select.OrderBy(item => item.Country);

            Repeater1.DataBind();
            LinkButton1.Visible = false;
            LinkButton3.Visible = true;
        }
    }

If you call the sortAscending() method the columns are arranged in ascending order and the output looks as

image3.gif

For the complete code please check the attached file.

In this post I have explained about How to bind repeater and sort the columns in the repeater using Linq.

Thanks !
 

Login to add your contents and source code to this article
share this article :
post comment
 

smart and simple ,easy to understand

Posted by Akila Akila Nov 15, 2010

smart and simple ,easy to understand

Posted by Akila Akila Nov 15, 2010

Nice one

Posted by Harshit Vyas Nov 12, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor