1

I'm trying to add sorting functionality to an old asp:Gridview and for some reason it is being a real pain to work with. I have tried multiple different things and failed. I would really appreciate some help with this. I have looked at examples at The Code Project and MSDN, but nothing is helping me. Here is my latest code:

<asp:SqlDataSource ID="grdVwCustomer_DataSource" runat="server" 
 ConnectionString="<%$ ConnectionStrings:DBConnectingString %>">
</asp:SqlDataSource>
<asp:Gridview ID="grdVwCustomer" runat="server" AutoGenerateColumns="false" 
  AllowSorting="true" DataKeyNames="CustomerID" OnSorting="grdVwCustomer_Sorting"
  DataSourceID="grdVwCustomer_DataSource">
 <asp:TemplateField HeaderText="User Name" SortExpression="UserName">
  <ItemTemplate>
     <%# DataBinder.Eval( Container.DataItem, "UserName" )%>&nbsp;
  </ItemTemplate>
 </asp:TempateField>
</asp:Gridview>

protected void Page_Load( object sender, EventArgs e )
    {
        string query = "SELECT * FROM vPanel_Customer WHERE DeletionStateCode=0";
        grdVwCustomer_DataSource.SelectCommand = query;
        grdVwCustomer.DataBind();
    }


protected void grdVwCustomer_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataBind(Convert.ToBoolean(e.SortExpression)); ****this line gets the error****
    }

EDIT:

 protected void grdVwCustomer_Sorting(object sender, GridViewSortEventArgs e)
    {
        switch (e.SortExpression)
        {
            case "UserName":
                if (e.SortDirection == SortDirection.Ascending)
                {
                    String queryString = "SELECT * FROM vPanel_Customer WHERE DeletionStateCode=0 ORDER BY UserName";
                    DataSet ds = GetData(queryString);
                    if (ds.Tables.Count > 0)
                    {
                        grdVwCustomer.DataSource = ds;
                        grdVwCustomer.DataBind();
                    }
                    else
                    {
                        //show message to user
                    }
                }
                break;
        }
    }



DataSet GetData(String queryString)
    {

        // Retrieve the connection string stored in the Web.config file.
        String connectionString = ConfigurationManager.ConnectionStrings["DBConnectingString"].ConnectionString;

        DataSet ds = new DataSet();

        try
        {
            // Connect to the database and run the query.
            SqlConnection connection = new SqlConnection(connectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);

            // Fill the DataSet.
            adapter.Fill(ds);
        }
        catch (Exception ex)
        {
            Logging.Log.LogException(Forbin.Logging.PageType.CustomerManage,   Logging.MessageType.Exception, ex.ToString());
            UIUtils.ShowMessageToUser("OnErrorMesg", this.Page);
        }
        return ds;
    }

The error Guid should be 32 digits... shows on this line CustomerID = new Guid(e.CommandArgument.ToString());

Jamie
  • 1,579
  • 8
  • 34
  • 74

1 Answers1

1

Your problem is the SortExpression your sort expression is SortExpression="UserName", yet you are trying to convert it to a boolean here Convert.ToBoolean(e.SortExpression)

UserName is not a boolean value, and of course will fail, for safe conversions (though not specifically to get it working for your scenario, you can do bool.TryParse("true", out myBoolVar)

Check out this MSDN article and this SO answer to get an idea on how to properly use SortExpression and allow sorting.

Community
  • 1
  • 1
Jason
  • 3,844
  • 1
  • 21
  • 40
  • Thanks, I added `case "UserName": if (e.SortDirection == SortDirection.Ascending){grdVwCustomer.DataSource = "SELECT * FROM vPanel_Customer WHERE DeletionStateCode=0 ORDER BY UserName"; grdVwCustomer.DataBind();}` to the code behind and now have the error `DataSource does not allow server-side paging` on the `DataBind()` line. Do I need something else in the case statement? – Jamie Dec 03 '13 at 17:06
  • @jlg you're trying to run serverside code, which it won't like. Try running the query separately and then binding the result set, like in this example, http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basedataboundcontrol.datasource%28v=vs.110%29.aspx, run the query get `DataSet` (though I believe you can do with a `DataTable` and even with a collection of `POCO`'s) then bind that to your grid. – Jason Dec 03 '13 at 17:15
  • I tried to use the example that you provided @Jason. I get the error `Guid should contain 32 digits with 4 dashes ....` I'll add my edit to the original post. – Jamie Dec 06 '13 at 21:02
  • @jlg it's obvious `e.CommandArgument.ToString()` is not a valid guid, as the error suggests, you need to debug and see what you are getting and why, I don't think it is at all related to how to use SortExpression – Jason Dec 09 '13 at 16:54
  • Oh. Ok. I will check it out. That part of the code has been working since it was built in 2008 so I wonder what happened. I assume it's bad data. Thanks – Jamie Dec 09 '13 at 17:38