【问题标题】:How can i get gridview row count我怎样才能获得gridview行数
【发布时间】:2015-10-02 18:45:09
【问题描述】:

我正在尝试获取 gvProducts 行数。

我已经尝试了以下代码,但结果不足。

 protected void gvProducts_RowCommand(object sender, GridViewCommandEventArgs e)
   {

        string commandName = e.CommandName.ToString().Trim();
        GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
        if (row.Controls.Count == 1)
        {
            //my code
        }
 }

【问题讨论】:

  • 您想要gridview 包含的行数还是该行所在的索引或该行包含的控件数?我对您的代码感到困惑,不知道您要获得多少计数。
  • 我想获取 gvProducts 行数
  • 然后接受@Rahul Singh 的回答,因为这正是他给你的

标签: c# asp.net gridview


【解决方案1】:

您想要 Gridview 中的总行数?使用Count属性:

gvProducts.Rows.Count

更新:

要查找嵌套gridview的行数,可以使用父gridview的RowDataBound事件:-

protected void gvProductsParent_RowCommand(object sender, GridViewCommandEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
        GridView gvProducts = (GridView)e.Row.FindControl("gvProducts ");
        int count = gvProducts.Rows.Count;
   }
}

请注意,此事件将为您的父网格视图中存在的每一行触发,count 将根据每一行而改变。

【讨论】:

  • @AntoinePelletier - 是的,我也选择了 OP 的标题。
  • 我收到以下错误,名称“gvProducts”在当前上下文中不存在
  • @ayman - 什么是gvProducts?它应该是您的网格视图的 ID。确保您在 gridview 标记定义中有runat="server"
  • id 是 gvProducts 及其嵌套的 gridview
  • 我也试过这段代码,但我得到错误:对象引用未设置为对象的实例。 GridViewRow gvRow = (GridViewRow)((Control)e.CommandSource).NamingContainer; Int32 rowIndex = gvRow.RowIndex; GridView gvProducts = gvRow.FindControl("gvProducts") as GridView; if (gvProducts.Rows.Count == 1)
猜你喜欢
  • 1970-01-01
  • 2011-06-04
  • 1970-01-01
  • 2017-05-25
  • 1970-01-01
  • 2018-04-05
  • 2023-03-21
  • 2017-08-12
  • 1970-01-01
相关资源
最近更新 更多