【问题标题】:String was not recognized as a valid boolean on sorting字符串在排序时未被识别为有效的布尔值
【发布时间】:2013-12-03 16:29:45
【问题描述】:

我正在尝试向旧的 asp:Gridview 添加排序功能,但由于某种原因,使用它确实很痛苦。我尝试了多种不同的方法,但都失败了。我真的很感激这方面的一些帮助。我查看了 The Code Project 和 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());

【问题讨论】:

  • 那么...问题出在哪一行?
  • 已编辑原始帖子以显示哪一行出现错误,感谢@Jason 指出这一点

标签: c# asp.net sorting gridview


【解决方案1】:

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

UserName 不是布尔值,当然会失败,为了安全转换(虽然不是专门让它适用于您的场景,您可以这样做 bool.TryParse("true", out myBoolVar)

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

【讨论】:

  • 谢谢,我在后面的代码中添加了case "UserName": if (e.SortDirection == SortDirection.Ascending){grdVwCustomer.DataSource = "SELECT * FROM vPanel_Customer WHERE DeletionStateCode=0 ORDER BY UserName"; grdVwCustomer.DataBind();},现在DataBind() 行出现错误DataSource does not allow server-side paging。我需要在案例陈述中添加其他内容吗?
  • @jlg 您正在尝试运行它不喜欢的服务器端代码。尝试单独运行查询,然后绑定结果集,如本例中的msdn.microsoft.com/en-us/library/…,运行查询获取DataSet(尽管我相信您可以使用DataTable 甚至使用POCO 的集合) s) 然后将其绑定到您的网格。
  • 我尝试使用您提供的示例@Jason。我收到错误 Guid should contain 32 digits with 4 dashes .... 我会将我的编辑添加到原始帖子中。
  • @jlg 很明显 e.CommandArgument.ToString() 不是一个有效的 guid,正如错误所暗示的,你需要调试并看看你得到了什么以及为什么,我认为它与如何使用 SortExpression
  • 哦。好的。我会看看。这部分代码自 2008 年构建以来一直在工作,所以我想知道发生了什么。我认为这是坏数据。谢谢
猜你喜欢
  • 1970-01-01
  • 2016-09-13
  • 2022-01-25
  • 2017-02-21
  • 1970-01-01
  • 2017-01-03
  • 2017-09-01
  • 2017-03-13
相关资源
最近更新 更多