在排序时,字符串未被识别为有效的布尔值

时间:2013-12-03 16:29:45

标签: c# asp.net sorting gridview

我正在尝试向旧的asp:Gridview添加排序功能,并且由于某种原因,使用它真的很痛苦。我尝试过多种不同的东西但都失败了。我真的很感激这方面的一些帮助。我查看了代码项目和MSDN中的示例,但没有任何帮助我。这是我最新的代码:

<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****
    }

修改

 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;
    }

错误Guid should be 32 digits...显示在此行CustomerID = new Guid(e.CommandArgument.ToString());

1 个答案:

答案 0 :(得分:0)

您的问题是 SortExpression 您的排序表达式为SortExpression="UserName",但您尝试将其转换为布尔值Convert.ToBoolean(e.SortExpression)

UserName不是一个布尔值,当然会失败,为了安全转换(虽然不是专门为它的方案工作,但你可以bool.TryParse("true", out myBoolVar)

查看此MSDN article和此SO answer,了解如何正确使用SortExpression并允许排序。