gridview.rows.cells.text returns empty even though there is data in the cells RRS feed

  • Question

  • User-1665848652 posted
    Sub RemoveDuplicates()
          Dim counter AS Integer
          Dim rows AS GridviewRowCollection   = myGridview_Users_ExtensionLists.Rows
          Dim curValue AS String = rows(0).Cells(0).Text
          FOR counter = 1 TO (myGridview_Users_ExtensionLists.Rows.Count - 1)
             IF curValue <> rows(counter).Cells(0).Text THEN
                curValue = rows(counter).Cells(0).Text
                Response.Write(rows(counter).Cells(0).Text & " " & counter & "<br />")
                rows(counter).Cells(0).Text = ""
                Response.Write(rows(counter).Cells(0).Text & "<br />")
             END IF
       END Sub

    This code correctly appends the row number and shows a list at the top of the page, but it does not show any of the data in the cells before the appended numbers. It allows me use .Text to replace the data with empty strings, but since it sees all of the .Text's as empty I am unable to do a check to see which ones I want to change. Any Ideas? I am calling this Sub in the body of my page just before the Gridview control is located.

    Wednesday, November 2, 2011 1:06 PM


  • User-1528094326 posted

    This only works for BoundFields. If you are using template fields then you need to access the labels or other controls directly in the cell by using the Cells(index).FindControl() method.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 2, 2011 2:31 PM