We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Conditionally setting Backcolor of cells

I would like to conditionally set the background color of a cell in a databound datagrid based on the value of the current cell compared to the cell 2 places before it. For example, the data in the row to be compared is: Forecast: 1 Actual: 2 When I use the following code, the grid is all white until i click somewhere on the grid, and eventually it errors out. Private Sub mysched_PrepareViewStyleInfo(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridPrepareViewStyleInfoEventArgs) Handles mySched.PrepareViewStyleInfo If e.RowIndex = 1 And e.Style.CellValue.ToString.StartsWith("A") Then If CInt(mySched.Model(1, e.RowIndex - 2).CellValue.ToString.Substring(10)) < CInt(e.Style.CellValue.ToString.Substring(8)) Then e.Style.BackColor = Color.green End If end sub I tried using the QueryCellInfo event, but that didn''t work so well either. If there are any suggestions I''d greatly appreciate it. Thanks Ryan

1 Reply

AD Administrator Syncfusion Team February 24, 2004 09:31 PM UTC

PrepareViewStyleInfo is the place you need to do this. I think you have to be more careful with teh indexes. If you want to reference 2 coluns prior, then the current column must be greater than 2 for this to make sense. Here is a try at this using aribitray rows > 0.
Private Sub mysched_PrepareViewStyleInfo(ByVal sender As Object, ByVal e As GridPrepareViewStyleInfoEventArgs) Handles mySched.PrepareViewStyleInfo
	If e.RowIndex > 0  And e.ColIndex > 2 Then
		if( e.Style.CellValue.ToString.StartsWith("A") Then
			If CInt(mySched.Model(e.RowIndex, e.ColIndex - 2).CellValue.ToString.Substring(10)) < CInt(e.Style.CellValue.ToString.Substring(8)) Then
				e.Style.BackColor = Color.green
			End If
		End If
	End If
end sub

Loader.
Up arrow icon