0
votes

I have a DataTable that gets data from an sql source and fills a gridview. Sorting from the sql statement is not an option as the order is important for doing some initial table manipulation.

I want a user to be able to click a column and have the gridview sort by the values in that column. I have googled and found many a guide to this, but none actually work for me. The columns are never clickable. I was able to achieve clickable rows that perform other actions with this bit of code, and I'm wondering if that leads to any interference, and if not, why can't I click to sort rows? Is it because my columns are not automatically generated?

//for clickable rows

if (e.Row.RowType == DataControlRowType.DataRow) { GridViewRow row = e.Row; DataRow data = ((DataRowView)row.DataItem).Row; e.Row.Attributes.Add("onmouseover","this.style.backgroundColor='#ceedfc'"); if (data.Field<string>("rowType") != "total") e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''"); e.Row.Attributes.Add("style", "cursor:pointer;"); e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(suiteReport, "Select$" + e.Row.RowIndex);

My attempt at sorting is

 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 suiteReport_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)
{

    DataTable dt = (DataTable)Session["QueryTable"];

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

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

which i based off of this

But, like I said, this seems to have no effect on the gridview. In the aspx file I have the attribute "AllowSorting" equal to true and OnSorting calls suiteReport_Sorting.

I'm not sure what the problem is, so any help would be appreciated.

Also, if there is any code that I did not include that you would like to see, please just let me know.

Thank you

EDIT: Here is how I handle the post back

`if (!Page.IsPostBack) {

            try
            {
                cn1.Open();

                cmd1 = new SqlCommand("sp_UpdateUsageLog", cn1);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.Add(new SqlParameter("@userName", SqlDbType.VarChar)).Value = userName;
                cmd1.Parameters.Add(new SqlParameter("@userIpAddress", SqlDbType.VarChar)).Value = userIpAddress;
                cmd1.Parameters.Add(new SqlParameter("@userComputerName", SqlDbType.VarChar)).Value = userComputerName;
                cmd1.Parameters.Add(new SqlParameter("@pageViewed", SqlDbType.VarChar)).Value = "Store Report Card";
                cmd1.Parameters.Add(new SqlParameter("@visitDate", SqlDbType.DateTime)).Value = DateTime.Now;

                cmd1.ExecuteNonQuery();
            }
            catch { }
            finally
            {
                cn1.Close();
            }

EDIT2: Here is the aspx for the gridview:

<asp:GridView ID="suiteReport" AutoGenerateColumns="false" ShowFooter="true" runat="server" OnRowDataBound="suiteReport_RowDataBound" OnSelectedIndexChanged="suiteReport_SelectedIndexChanged" AllowSorting="true" OnSorting="suiteReport_Sorting">

1
Are oyu capturing the postback event properly and not letting the page get written anew? - Dean.DePue
@Dean.DePue I do have a catch for Page.IsPostBack, but what do you mean "not letting the page get written anew"? - atallest
What do you do when the it is a postback? Does the exact same sort appear in the grid? - Dean.DePue
@Dean.DePue I believe so, yes. Clicking various buttons only changes which rows are displayed, but the data table remains unchanged and in the same order. - atallest
What does the code look like when it is a postback? - Dean.DePue

1 Answers

0
votes

I believe I've figured it out, or at least made ample progress. The reason my header rows weren't clickable was because I had neglected to add SortExpressions to the bound fields. Since I was building columns dynamically I accomplished this by looping through the headers in the gridview, creating a new boundfield (this I had done before) and also giving the BoundField a SortExpression (myBoundField.SortExpression = "headerName")

Hopefully this helps anyone that was also stuck.