Sorting in Repeater Control in a simple way using Linq


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 !
 


Similar Articles