簡體   English   中英

如何計算gridview asp.net中的行數c#

[英]How to count the rows in a gridview asp.net c#

我有一個正確填充的gridview。 當用戶按下“搜索”按鈕時,將調用函數ShowData(),該函數根據輸入到文本框中的條件顯示數據。 我希望能夠計算gridview中創建的行數。

我知道我可以從SQL數據庫表中計算它,但我覺得簡單地計算gridview中的行會更容易! (如果我錯了,請糾正我!)

我試過使用search_results = GridView1.Rows.Count.ToString(); 在具有此標記的ShowData()函數中的代碼中:

<div class="size_info" id="allSelected" runat="server" visible="false"><br />
                There are <asp:Label ID="search_results" runat="server"></asp:Label> entries for this search criteria.  
</div>
</div>

但它似乎沒有返回任何值(甚至不是0!)。

非常感激任何的幫助!

search_results = GridView1.Rows.Count.ToString();

將編譯錯誤作為

System.String無法轉換為System.Web.UI.Label

使用

search_results.Text = GridView1.Rows.Count.ToString();

並確保在綁定網格后寫入此內容。

如果您啟用了Pagination ,通過使用GridView1.Rows.Count您將只獲取當前頁面行,因此如果PageSize等於10並且您有60行,那么結果將得到10,這是不正確的!

在這種情況下,你需要這樣:

protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        var d = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
        if (d != null) lblGridRowsCount.Text = d.Count.ToString();
}

暫無
暫無

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

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