簡體   English   中英

字符串在排序時未被識別為有效布爾值

[英]String was not recognized as a valid boolean on sorting

我試圖將排序功能添加到舊的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());

您的問題是SortExpression您的排序表達式是SortExpression="UserName" ,但是您正在嘗試將其轉換為布爾值Convert.ToBoolean(e.SortExpression)

UserName不是布爾值,並且為了安全的轉換,當然會失敗(盡管並不是專門為了使它適合您的情況,您可以通過bool.TryParse("true", out myBoolVar)執行bool.TryParse("true", out myBoolVar)

請查看此MSDN文章和此SO答案 ,以了解如何正確使用SortExpression並允許排序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM