簡體   English   中英

在過濾器表達式后檢查gridview計數

[英]checking gridview count after filter expression

我正在研究一個asp:GridView。 在此aspx頁面上,我為用戶提供了通過在文本框中鍵入特定值來搜索當前gridview的選項。 然后,根據用戶輸入的內容設置SqlDataSource.FilterExpression。

例如,用戶鍵入“ MODEL1”並點擊搜索。 然后:

SqlDataSource.FilterExpression = "ModelName= " + "'" + TextBox.Text + "'";

接下來,我需要檢查是否得出結果? 顯然,我可以告訴它是否不在頁面上,因為沒有可見的gridview。 但是,我需要一種捕獲它的方法,以便可以重置FilterExpression並向用戶拋出“錯誤搜索”消息。 我沒有在FilterExpression下看到任何對我有幫助的方法,並且我嘗試了GridView.Rows.Count,但它似乎不起作用。 我是否還有其他選擇,或者行方法是否做錯了?

這是觸發搜索事件時發生的一些額外代碼:

        if (TextBox.Text == string.Empty)
        {
            errorLabel.Text = "Please enter a value";
        }
        else
        {
            SqlDataSource.FilterExpression = "ModelName= " + "'" + TextBox.Text + "'";
            if (!GridView.Rows.Count > 0)
            {
                errorLabel.Text = "Model not found";
                SqlDataSource.FilterExpression = "";
            }
        }

編輯:只是想添加GridView.Rows.Count產生值51,這是沒有FilterExpression存在的行數。

將結果放入具有Count屬性的DataView

SqlDataSource.FilterExpression = "ModelName= " + "'" + TextBox.Text + "'";
System.Data.DataView dv = (DataView)SqlDataSource.Select(DataSourceSelectArguments.Empty);
dv.RowFilter = SqlDataSource.FilterExpression;
int RowCount = dv.Count;

我建議您在FilterExpression上使用String.Format() 它使您避免語法錯誤:

SqlDataSource.FilterExpression = String.Format("ModelName='{0}'", TextBox.Text);

暫無
暫無

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

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