Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Color DataGridView cell on exception

Hi,
I'm using VB.NET 2005, and would like to color a cell on a DataGridView if an exception has been invoked. I have the following code snippet:

' ############ GET THE NEW 'FROM' DATE ############

Try
tblRow.Item("From") = Convert.ToDateTime(strNewFromDate)

Catch ex As Exception

' Problem with this date field, so leave blank

' Add 1 to the number of warnings count
intNumberOfWarnings += 1

' Colour the cell in error

End Try

Is there a clever bit of code I need to use that will allow me to color this particular cell?
[654 byte] By [jellyfish5000] at [2007-11-11 10:29:25]
# 1 Re: Color DataGridView cell on exception
This is just a starting point for you. You'll have to tweak it a bit to make it work for you.

dgv.CurrentRow.DefaultCellStyle.BackColor = Color.BlanchedAlmond

this will at least highlight the entire row. Play around with currentrow.currentcell, might be what your looking for. Hope this helps.

:)

jb
jcb1269 at 2007-11-11 20:47:38 >
# 2 Re: Color DataGridView cell on exception
Thanks for the tip.
I've collected the rows and cells "in error" in an array, and I've looped through the array with the following line:

Me.DataGridView1.Rows(arWarningRow(intArrayPosition)).Cells(arWarningCell(intArrayPosition )).style.BackColor = Color.Yellow
Works well!
jellyfish5000 at 2007-11-11 20:48:39 >