user130622
user130622

Reputation: 53

Sort a function in c# gridview

I am building a small webapp to keep track of win/loss/average. The data is being saved in a XML file (since its just a couple lines). I want to try and give the visitor of the page, the ability to Sort by every column possible (Played, Won, Average). I am able to get it to sort Played, and Won, but not Average, because Average is a calculated value.

How do I get it to sort the results of the GetWinAverage function?

This is how it currently looks:

<asp:GridView ID="Results" runat="server" AutoGenerateColumns="false" AllowSorting="true" OnSorting="gvResults_Sorting" EnableViewState="true">
        <Columns>
            <asp:TemplateField HeaderText="Played" SortExpression="Played">
                <ItemTemplate>
                    <asp:Label ID="lblPlayed" runat="server" Text='<%# Eval("Played") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <Columns>
            <asp:TemplateField HeaderText="Won" SortExpression="Won">
                <ItemTemplate>
                    <asp:Label ID="lblWon" runat="server" Text='<%# Eval("Won") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <Columns>
            <asp:TemplateField HeaderText="Average" SortExpression="Average">
                <ItemTemplate>
                    <asp:Label ID="lblAverage" runat="server" Text='<%# GetWinAverage(Eval("Won"), Eval("Played")) %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>

Where GetWinAverage looks like this:

public string GetWinAverage(object gamesWon, object gamesPlayed)
    {
        string strReturn = "";
        double iWon = Convert.ToDouble(gamesWon);
        double iPlayed = Convert.ToDouble(gamesPlayed);
        if (iPlayed < 0)
        {
            return "0%";
        }
        else
        {
            double dReturn = (iWon / iPlayed) * 100;
            strReturn = string.Format("{0:N2}%", dReturn);
            return strReturn;
        }
    }

And Sorting looks like this:

private const string ASCENDING = " ASC";
    private const string DESCENDING = " DESC";

    public SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;

            return (SortDirection)ViewState["sortDirection"];
        }
        set { ViewState["sortDirection"] = value; }
    }

    protected void gvResults_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;

        if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            SortGridView(sortExpression, DESCENDING);
        }
        else
        {
            GridViewSortDirection = SortDirection.Ascending;
            SortGridView(sortExpression, ASCENDING);
        } 
    }

    private void SortGridView(string sortExpression, string direction)
    {
        //  You can cache the DataTable for improving performance
        DataTable dt = dtPlayers;

        DataView dv = new DataView(dt);
        dv.Sort = sortExpression + direction;

        gvResults.DataSource = dv;
        gvResults.DataBind();
    }

Upvotes: 0

Views: 440

Answers (1)

Conrad Frix
Conrad Frix

Reputation: 52675

The easiest way might be just to modify your DataTable to use a Calculated data column.

You can do this by adding the column "Average" and setting the DataColumn.Expression property to be Won/Played * 100. Then bind to it as you would any other column.

Upvotes: 1

Related Questions