【问题标题】:Need help looping through multiple gridviews on a page c#.net需要帮助在页面 c#.net 上循环多个网格视图
【发布时间】:2017-04-07 06:10:28
【问题描述】:

我需要帮助遍历页面上的 2 个网格视图。最终,我将在页面上总共有 6 个网格视图(或部分)。每个 gridview 都有相同的列,但每个都有不同的行数。迭代的目的是计算当前gridview迭代中gridview总行中一列中选择的radiobuttonlist项目的总数,以便信息可以显示在提交按钮单击的标签中,如下所示: “第 1 部分中的 14 个评分中有 10 个已检查。”“第 2 部分中的 11 个评分中的 11 个已检查。” 或任何情况。

我已经用谷歌搜索了到目前为止的代码,并关注了这个帖子,包括聊天How to use a for loop to modify multiple controls (gridview)
但是我不明白如何处理这行代码。省略号后面是什么??它适用于我的情况吗?

((GridView) Page.FindControl("GridView" + i)).Rows[1]......... 

当我添加.Rows[j](或1)时,.Rows 上会出现一条红色波浪线...并发出警告:system.web.ui.control 不包含“行”的定义,并且找不到接受 system.web.ui.control 类型的第一个参数的扩展方法“行”(您是否缺少 using 指令或程序集引用?)。我有using System.Linq;我页面顶部的声明。 这是我通过gridviews计数的代码。 GridView id 是 GridView1、GridView2 等。GridView 使用 sqldatasource 填充并包含绑定和模板字段列。如果我通过 在浏览器中查看 运行代码,则会在这行代码中抛出错误“对象引用未设置为对象的实例”:

foreach (GridViewRow row in gv.Rows);

但在调试期间,它似乎识别出正确的 GridView,因为它获得了正确的行数和选择了正确的单选按钮列表项。

代码:

    //UPDATED code includes solution:
protected void CountSelectedRatings()
        {
            int count = 0;
            int itemsSelected = 0;
            string str = string.Empty;
            int i = 1;

            //loop through 6 gridviews
            for (i = 1; i <= 6; i++)
            {
                //identify current grid to iterate through 
                GridView gv = (GridView)Page.FindControl("GridView" + i);

                itemsSelected = 0;

                if (gv != null)
                { 
                   foreach (GridViewRow row in gv.Rows) 
                    {
                        count = gv.Rows.Count; 


                            RadioButtonList rbl = row.FindControl("rblRating" + i) as RadioButtonList;

                            foreach (ListItem item in rbl.Items)
                            {
                                if (item.Selected == true)
                                {
                                    //increment count
                                    itemsSelected += 1;

                                    switch (i)
                                    {
                                        case 1:
                                            strRowCountMessage1 = itemsSelected + " of " + count + " ratings in Section " + i + " are checked.";
                                            lblRowCountMessage1.Text = strRowCountMessage1;
                                            break;

                                        case 2:
                                            strRowCountMessage2 = itemsSelected + " of " + count + " ratings in Section " + i + " are checked.";
                                            lblRowCountMessage2.Text = strRowCountMessage2;
                                            break;

                                        //case 3:
                                        //    strRowCountMessage3 = itemsSelected + " of " + count + " ratings in Section " + i + " are checked.";
                                        //    lblRowCountMessage3.Text = strRowCountMessage3;
                                        //    break;

                                        //case 4:
                                        //    strRowCountMessage4 = itemsSelected + " of " + count + " ratings in Section " + i + " are checked.";
                                        //    lblRowCountMessage3.Text = strRowCountMessage4;
                                        //    break;

                                        //case 5:
                                        //    strRowCountMessage5 = itemsSelected + " of " + count + " ratings in Section " + i + " are checked.";
                                        //    lblRowCountMessage3.Text = strRowCountMessage5;
                                        //    break;

                                        //case 6:
                                        //    strRowCountMessage6 = itemsSelected + " of " + count + " ratings in Section " + i + " are checked.";
                                        //    lblRowCountMessage3.Text = strRowCountMessage6;
                                        //    break;
                                    }//end switch

                                }//end if item.Selected = True 

                        }//end foreach ListItem 

                    }//end foreach gridviewrow
                }//end if gv !=null  
            }// end for loop i

            GridView1.DataBind();
            GridView2.DataBind();
            //GridView4.DataBind();
            //GridView5.DataBind();
            //GridView6.DataBind();

    }

【问题讨论】:

  • 看看这是否有帮助:stackoverflow.com/questions/28875542/…>.
  • 感谢您对我的问题感兴趣。该链接是关于查找单选按钮列表项的值,但我的代码已成功获取该信息。我的代码在“foreach(gv.Rows 中的 GridViewrow 行)”处出现错误,如果让其运行但在调试期间找到正确的网格和单选按钮列表项。
  • 我认为问题与不首先检查空网格有关......

标签: c# gridview asp.net-3.5


【解决方案1】:

我最初的问题的解决方案是在 foreach 循环之前检查 null gridview。下面是该部分代码的摘录。我修改了我的原始代码以反映最终代码,该代码使用单选按钮对总行数进行计数并显示在标签中。

        for (i = 1; i <= 6; i++)
        {
            //identify current grid to iterate through 
            GridView gv = (GridView)Page.FindControl("GridView" + i);

            itemsSelected = 0;

            if (gv != null)//<--
            { 

               foreach (GridViewRow row in gv.Rows) 
                {//code continues here}}

【讨论】:

    猜你喜欢
    • 2021-02-16
    • 1970-01-01
    • 2021-05-14
    • 2013-06-17
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    相关资源
    最近更新 更多