Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Sorting GridView (datasource = custom collection)

Posted on 2011-02-15
1
526 Views
Last Modified: 2012-05-11
I have a GridView binded to a custom collection. How can I implement GridView sorting?

The gridview displays an user log. I need to sort the gridview by clicking the gridview headings. For example: When clicking the "Attempts" header I need to sort the gridview by "Attempts" (Asc/Desc).

Please see attached code. Thanks for all help!

<asp:GridView runat="server" ID="grdLog" AutoGenerateColumns="false" OnPageIndexChanging="grdLog_PageIndexChanging" AllowSorting="true" AllowPaging="true" PageSize="50">
    <Columns>
        <asp:TemplateField HeaderText="Id">
            <ItemTemplate>
                <%# DataBinder.Eval(Container.DataItem, "Id") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="User">
            <ItemTemplate>
                <%# DataBinder.Eval(Container.DataItem, "User") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Attempts" SortExpression="Attempts">
            <ItemTemplate>
                <%# DataBinder.Eval(Container.DataItem, "Attempts") %>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>



protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        BindLog();
    }
}


protected void BindLog()
{
    LogCollection Log = LogHandler.GetLog();
    this.grdLog.DataSource = Log;
    this.grdLog.DataBind();
}


protected void grdLog_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    this.grdLog.PageIndex = e.NewPageIndex;
    BindLog();
}

Open in new window

0
Comment
Question by:webressurs
1 Comment
 
LVL 16

Accepted Solution

by:
Swapnil Piparia earned 500 total points
ID: 34895233
Hi webressurs,

Below article help you to implement sorting with custom collection in gridview.

http://www.codeproject.com/KB/webforms/Strongly_typed_collection.aspx

Thanks,
netswap
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question