Help with looping through Grid control and check ckeckbox

vcharles
vcharles used Ask the Experts™
on
Hi,

I used the code below with a combobox to check checkboxes depending on utubeItemA (utubeItemA = 0,1,2 would cause checkboxes in first three rows to be checked)

How do I modify the code to do the same with the GridView control? Items is not a property of the GridView control.

  If utubeItemA IsNot Nothing Then
            For i As Integer = 0 To C1ItemA.Items.Count - 1
                If utubeItemA.Split(",").Contains(C1ItemA.Items(i).Text) Then
                    C1ItemA.Items(i).Selected = True
                    Dim CB As CheckBox = C1ItemA.Items(i).FindControl("CheckItemA")
                    CB.Checked = True
                End If
            Next
        End If

Thanks,

Victor
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
Use Rows

Grid.Rows(rowindex).Item(columnindex)
Meir RivkinFull stack Software Engineer

Commented:
do something like:

 For i As Integer = 0 To gridView.Rows.Count - 1
            Dim cb As CheckBox = CType(gridView.Rows(i).FindControl("YourCheckBox"), CheckBox)
If utubeItemA.Split(",").Contains(gridView.Rows(i).Text) Then
            cb.Checked = True
  End If
        Next

Open in new window

Author

Commented:
Thanks, If I am not using the checkbox, How do I just highlight the row?
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Full stack Software Engineer
Commented:
Dim tokens As String() = utubeItemA.Split(",")

For Each row As DataGridViewRow In gridView.Rows
           If tokens.Contains(row.Text) Then
row.DefaultCellStyle.BackColor = Color.Red
  End If
Next

Open in new window

Most Valuable Expert 2012
Top Expert 2014

Commented:
You can also set the row to Selected

Author

Commented:
Thanks I will try them and get back to you.

Author

Commented:
Thank You!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial