Cell color based on cell value ( % )

3 posts, 1 answers
  1. Mahir
    Mahir avatar
    5 posts
    Member since:
    Jan 2017

    Posted 03 Nov 2017 Link to this post

    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. Mahir
    Mahir avatar
    5 posts
    Member since:
    Jan 2017

    Posted 03 Nov 2017 Link to this post

    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 ).
  3. Answer
    Dimitar
    Admin
    Dimitar avatar
    2525 posts

    Posted 06 Nov 2017 Link to this post

    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.
Back to Top