views:

21

answers:

0

I have a gridview that is bound to a datatable. I have also written some c# code to sort and page that gridview. However, the buttonfield column is not being sorted with the rest of the table. How would I accomplish this?

Current Sorting Code:

    protected void gridView_SortingUser(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = GridViewUsers.DataSource as DataTable;

        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);

            GridViewUsers.DataSource = dataView;
            GridViewUsers.DataBind();
        }
    }

    private string GetSortDirection(string column)
    {
        // By default, set the sort direction to ascending.
        string sortDirection = "DESC";

        // Retrieve the last column that was sorted.
        string sortExpression = ViewState["SortExpression"] as string;

        if (sortExpression != null)
        {
            // Check if the same column is being sorted.
            // Otherwise, the default value can be returned.
            if (sortExpression == column)
            {
                string lastDirection = ViewState["SortDirection"].ToString();
                if ((lastDirection != null) && (lastDirection == "DESC"))
                {
                    sortDirection = "ASC";
                }
            }
        }
        ViewState["SortDirection"] = sortDirection;
        ViewState["SortExpression"] = column;
        return sortDirection;
    }

This is how it is bound:

            DataTable dtUsers = new DataTable();
            dtUsers = FillTable();
            GridViewUsers.DataSource = dtUsers;
            GridViewUsers.DataBind();

Here is how the gridview is setup:

                    <asp:GridView ID="GridViewUsers" runat="server" AllowPaging="True" 
                        OnPageIndexChanging="gridView_PageIndexChangingUser" CellSpacing="1" 
                        CssClass="contentHomeTabOrdersGrid" AllowSorting="True" 
                        OnSorting="gridView_SortingUser" PageSize="13" 
                        OnSelectedIndexChanged="GridViewUsers_SelectedIndexChanged">
                        <AlternatingRowStyle BackColor="#DDE1FF" />
                        <columns>
                            <asp:buttonfield buttontype="Image" 
                            commandname="Select" Text="Go" ImageUrl="images/Arrow.png">
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:buttonfield>
                        </columns>
                        <HeaderStyle Wrap="False" />
                        <RowStyle Height="22px" />
                    </asp:GridView>