[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

Code removing the wrong cells - how do I get the value of a cell in GridView

Hi I'm trying to add rowspan to the first cell in a group and then removing the subsequent cells but my code seems to be removing the wrong cells (e.g gvr.Cells.RemoveAt(0);).  How can I get the value of a cell in the following code so I can determine which cells are actually being removed??

foreach (GridViewRow gvr in gvWeeksCatches.Rows)
        {
            gvRowIndex = gvr.RowIndex;
            if (gvRowIndex == firstSpanRow)
            {
                gvr.Cells[0].RowSpan = numSpanRows;
                gvr.Cells[1].RowSpan = numSpanRows;
            }
            //if row is in rowspan group
            else if (gvRowIndex > firstSpanRow && gvRowIndex <= lastSpanRow)
            {
                gvr.Cells.RemoveAt(0);
                gvr.Cells.RemoveAt(1);
               
            }
            gvRowIndex++;

        }
0
Lorna70
Asked:
Lorna70
  • 2
1 Solution
 
lojk.Net and Infrastructure ConsultantCommented:
your code here

else if (gvRowIndex > firstSpanRow && gvRowIndex <= lastSpanRow)
            {
                gvr.Cells.RemoveAt(0);
                gvr.Cells.RemoveAt(1);
               
            }

is not doing what you think it is...

It is removing the first cell, then it is removing the *second* cell from that which remain (i.e. the first statement changes the list that you are operating upon).

to remove the first 2 cells you should do (my preference)

  {
                gvr.Cells.RemoveAt(0);
                gvr.Cells.RemoveAt(0);
               
            }

or even this would work better than what you have

  {
                gvr.Cells.RemoveAt(1);
                gvr.Cells.RemoveAt(0);
               
            }


if you think about it - it makes more sense..

Try laying out some playing cards on the table and replicating your code operations if you still dont understand what i mean.
0
 
Lorna70Author Commented:
Thank you so much - I've been tearing my hair out for weeks on this! :-)
0
 
lojk.Net and Infrastructure ConsultantCommented:
It was a bit of a glaring error but I've been down this road with similar issues so many times before - sometimes you just need a fresh pair of eyes on the subject.

Glad to have saved some of your hair, even if it's too late for mine. ;-)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now