How can i get gridview row count

北慕城南 提交于 2020-12-08 08:01:50

问题


I am trying to get gvProducts row count.

I have tried the below code but it give insufficient result.

 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
        }
 }

回答1:


You want total rows in Gridview? Use Count property:

gvProducts.Rows.Count

Update:

To find the rows count of nested gridview, you can use the RowDataBound event of parent gridview:-

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;
   }
}

Please note this event will fire for each row present in your parent gridview this the count will change according to each row.



来源:https://stackoverflow.com/questions/32913837/how-can-i-get-gridview-row-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!