0

I am trying to figure out which column (as an integer) I am in as I loop through each cell:

For Each cell As TableCell In e.Row.Cells
                If caseType = 0 Then
                    cell.BackColor = Color.Red
                ElseIf caseType = 1 Then
                    cell.BackColor = Color.Blue
                End If
            Next

Basically, I want to perform a different operation based on which column I am currently in for a given row...

Edit:

I added a counter for each time through the loop thinking I could use that to figure out which column I'm in:

For Each cell As TableCell In e.Row.Cells
            System.Diagnostics.Debug.WriteLine(i)                
            If caseType = 0 Then
                cell.BackColor = Color.Red
            ElseIf caseType = 1 Then
                cell.BackColor = Color.Blue
            End If
            i = i + 1
        Next

My table ONLY HAS 13 columns, but the Writeline is printing out 0 - 15... any idea where the extra three columns are coming from?

user1801932
  • 359
  • 1
  • 4
  • 20
  • 1
    seems you need something like this http://stackoverflow.com/questions/9349620/how-to-access-a-gridview-column-on-rowdatabound – ale Jun 25 '14 at 05:11
  • no, that doesn't help me... I'm trying to get the actual number of the column I am sitting in... not the header name... my table has 13 columns... so I want to be able to loop through and get at the actual number (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) – user1801932 Jun 25 '14 at 05:29
  • Sorry, I'm not understand, you are in the rowdatabound event, if you use a for instead foreach what is the problem? – ale Jun 25 '14 at 05:40
  • Well the header solution is actually better... because if I add a datagrid element later, i won't have to worry about changing my code... because the header for each column will stay the same... – user1801932 Jun 25 '14 at 06:38

1 Answers1

0

Infer-On was actually right, just needed a second to interpret code... My column headers are ALWAYS THE SAME, so I could check the headertext versus the column number:

For Each cell As TableCell In e.Row.Cells
                Header = GridView1.HeaderRow.Cells(i).Text
                System.Diagnostics.Debug.WriteLine(Header)
                If caseType = 0 Then
                    cell.BackColor = Color.Red
                ElseIf caseType = 1 Then
                    cell.BackColor = Color.Blue
                End If
                i = i + 1
            Next

Now if I'm in a particular column, I can do my dirty work!

Thanks!

user1801932
  • 359
  • 1
  • 4
  • 20