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
close icon

Conditional formatting

Hello,

I have a GridControl with 3 columns. Cells in column C contains a conditional formula that return only two values: 0 or 1. I want that the format of column B changes depending on the value of column C.

This it is the algorithm:

If C1=1 then B1.ReadOnly=True : B1.BackColor=Gray ....
Else B1.ReadOnly=False : B1.BackColor=White ....

If C2=1 then B2.ReadOnly=True : B2.BackColor=Gray ....
Else B2.ReadOnly=False : B2.BackColor=White ....

If C3=1 then B3.ReadOnly=True : B3.BackColor=Gray ....
Else B3.ReadOnly=False : B3.BackColor=White ....

Something like this already I try:

Private Sub GridControl1_CurrentCellDeactivated(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridCurrentCellDeactivatedEventArgs) Handles GridControl1.CurrentCellDeactivated
If e.ColIndex = 2 Then
For i = 1 To GridControl1.RowCount
If GridControl1(i, 3).FormattedText = "1" Then
GridControl1(i, 2).ReadOnly = False
GridControl1(i, 2).BackColor = Color.Gray
GridControl1(i, 2).Text = "NA"
GridControl1(i, 2).ReadOnly = True
Else
GridControl1(i, 2).ReadOnly = False
GridControl1(i, 2).BackColor = Color.White
If GridControl1(i, 2).Text = "NA" Then
GridControl1(i, 2).Text = ""
End If
End If
Next
End If
End Sub

But it does not work well.


I saw that in your Web page you have a sample named "Formatting Cells Based on Criteria", I think that is seemed to that I need, but I not where to find it.



Best Regards,
Jorge

P.D. I do not speak very well English, it excuses if I am mistaken.

ConditionalFormattingSample.zip

3 Replies

AD Administrator Syncfusion Team September 14, 2006 04:51 AM UTC

Hi Jorge,

To dynamically set style properties on a cell, the PrepareViewStyleInfo event has to handled. We can access the data through Model object in the row and after checking conditions, we can change the style of the row. Below is a code snippet.

Private Sub GridControl1_PrepareViewStyleInfo(ByVal sender As System.Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridPrepareViewStyleInfoEventArgs) Handles GridControl1.PrepareViewStyleInfo
If e.RowIndex > 0 And e.ColIndex = 2 Then
If GridControl1(e.RowIndex, 3).FormattedText = "1" Then
e.Style.ReadOnly = True
e.Style.BackColor = Color.Gray
e.Style.Text = "NA"
Else
e.Style.ReadOnly = False
e.Style.BackColor = Color.White
If e.Style.Text = "NA" Then
e.Style.Text = ""
End If

End If
End If
End Sub

Here is a modified sample.
http://www.syncfusion.com/Support/user/uploads/ConditionalFormattingSample_7e1aef08.zip

Best Regards,
Haneef


JO Jorge September 14, 2006 03:38 PM UTC

Thank you for your quick answer,

I think that now I am oriented to the corrected way. Nevertheless, I notice that the PrepareViewStyleInfo event is invocated when the cell receives the focus or when grid is drawn, so the format of the cells not always is updated when the value of column C changes. There is some way to change this behavior?

Thank you for your patient,

Best regards,
Jorge


JO Jorge September 14, 2006 04:05 PM UTC

Hi Haneef

I think that I have found the solution, simply I added the following line of code to the event CurrentCellDeactivate:

GridControl1.Refresh

Is that the best way to do it?

Thank you,
Jorge

Loader.
Live Chat Icon For mobile
Up arrow icon