Need help looping through multiple gridviews on a page c#.net

时间:2017-04-06 17:16:43

标签: c# gridview asp.net-3.5

I need help iterating through 2 gridviews on a page. Ultimately I will have 6 total gridviews (or Sections) on the page. Each gridview is has same columns, but each will have a different number of rows. The objective of the iteration is to count the total number of radiobuttonlist items selected in one column out of the total gridview rows in current gridview iteration so the information can be displayed in a label on the Submit button click like so: “10 of 14 ratings in Section 1 are checked." Or “11 of 11 ratings in Section 2 are checked." Or whatever the case may be.

I have googled to come up with the code I have so far, and followed this thread including the chat How to use a for loop to modify multiple controls (gridview)
But I don’t understand what to do with this line of code. What goes after the ellipsis?? Does it apply in my situation?

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

When I add the .Rows[j] (or 1), a red squiggly line appears on .Rows… with warning: system.web.ui.control does not contain a definition for 'Rows' and no extenstion method 'Rows' accepting the first argument of type system.web.ui.control could be found (are you missing a using directive or assembly reference?). I do have using System.Linq; statement at the top of my page. Here is my code to count through gridviews. GridView id's are GridView1, GridView2, etc. GridViews are filled using sqldatasource and contain both bound and template field columns. If I run the code via View in Browser it throws error “Object reference not set to an instance of an object” on this line of code:

foreach (GridViewRow row in gv.Rows);

During debug though, it seems to recognize the correct GridView because it is getting the correct row count and the correct radiobuttonlist items selected.

code:

    //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();

    }

1 个答案:

答案 0 :(得分:0)

我原来问题的解决方案是在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}}