This is a migrated thread and some comments may be shown as answers.

Cell color based on cell value ( % )

2 Answers 246 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mahir
Top achievements
Rank 1
Mahir asked on 03 Nov 2017, 12:37 PM

Hi guys, how are You. 

I have one quesition and I hope You guys can help me.

Ok, datasource in my radgridview is DataTable( filled with DataAdapter / query). < - I have to use this because quety is using PIVOT  construct.

You can see the results of my query in attachment. Now You see that cells with the values? its 3 , 20, 8 , 10 etc. Now i want to change the color or what cell based on that value. Ex. if the cell value is 20 then 20 % of that cell should be Color.Red and so on, cell with Null value or 0 value should stay the same color ( default one ). 

I hope You guys will understand my question.

 

2 Answers, 1 is accepted

Sort by
0
Mahir
Top achievements
Rank 1
answered on 03 Nov 2017, 12:43 PM
Btw, about p language, c# or vb.net ( this is done in vb.net, but if You know how to do this in C# is great ).
0
Accepted
Dimitar
Telerik team
answered on 06 Nov 2017, 01:41 PM
Hello Mahir,

You can use the custom painting to achieve this. Here is an example:
Private Sub RadGridView1_CellPaint(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellPaintEventArgs)
    If e.Cell.Value IsNot Nothing AndAlso e.Cell.ColumnIndex = 0 AndAlso TypeOf e.Cell.RowInfo Is GridViewDataRowInfo Then
        Dim value As Single = Convert.ToSingle(e.Cell.Value)
        If value = 0 Then
            Return
        End If
        Dim brush As Brush = If(value < 20, Brushes.Red, Brushes.Green)
        Dim percentage = (e.Cell.Bounds.Width * (value /100))
        e.Graphics.FillRectangle(brush, New RectangleF(0, 0, CSng(percentage), e.Cell.Bounds.Height))
 
    End If
End Sub

The attached image shows the result on my side. Another approach is to add a progress bar to the cell: Creating Custom Cells.

I hope this will be useful. Let me know if you have additional questions.

Regards,
Dimitar
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
GridView
Asked by
Mahir
Top achievements
Rank 1
Answers by
Mahir
Top achievements
Rank 1
Dimitar
Telerik team
Share this question
or