Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sorting GridView (datasource = custom collection)

Posted on 2011-02-15
1
Medium Priority
?
534 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

963 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