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

Issues with setting background color in CellFormating of radGridView

5 Answers 418 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mike Baldini
Top achievements
Rank 1
Mike Baldini asked on 26 Aug 2010, 11:37 PM
I have a radgridview that I want to change the background color of three columns depending on the value of the cells. The following code works great for changing the colors of the cells based on its value, however if i click into the cell all the formatting for that cell is lost. Is there something we are missing here?

Private Sub rgvTimeCards_CellFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles rgvTimeCards.CellFormatting
        Dim amtIndex As Integer = rgvTimeCards.Columns("BillableAmount").Index
        Dim hrsIndex As Integer = rgvTimeCards.Columns("MarkUpHrs").Index
        Dim dollarsIndex As Integer = rgvTimeCards.Columns("MarkUpDollars").Index
        If e.CellElement.ColumnIndex.Equals(hrsIndex) Or e.CellElement.ColumnIndex.Equals(dollarsIndex) Then
            If rgvTimeCards.Rows(e.CellElement.RowIndex).Cells(e.CellElement.ColumnIndex).Value < 0 Then
                e.CellElement.NumberOfColors = 2
                e.CellElement.BackColor = Color.White
                e.CellElement.BackColor2 = Color.Pink
                e.CellElement.ForeColor = Color.Black
                e.CellElement.DrawFill = True
            ElseIf rgvTimeCards.Rows(e.CellElement.RowIndex).Cells(e.CellElement.ColumnIndex).Value > 0 Then
                e.CellElement.NumberOfColors = 2
                e.CellElement.BackColor = Color.White
                e.CellElement.BackColor2 = Color.FromArgb(219, 230, 145)
                e.CellElement.ForeColor = Color.Black
                e.CellElement.DrawFill = True
            Else
                e.CellElement.DrawFill = False
            End If
        ElseIf e.CellElement.ColumnIndex.Equals(amtIndex) Then
            If rgvTimeCards.Rows(e.CellElement.RowIndex).Cells(e.CellElement.ColumnIndex).Value < 0 Then
                e.CellElement.NumberOfColors = 2
                e.CellElement.BackColor = Color.White
                e.CellElement.BackColor2 = Color.Pink
                e.CellElement.ForeColor = Color.Black
                e.CellElement.DrawFill = True
            Else
                e.CellElement.DrawFill = False
            End If
        End If
    End Sub

5 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 27 Aug 2010, 07:19 AM
Hello Mike,

What do you mean by the formatting is lost ? Just while the row is selected? Of even after you deselect that row?

If the answer is the yes to the while that row is selected you should use Conditional Formatting,
else
try their documentation pages for the RowFormatting and for the CellFormatting

Hope this helps,
Best Regards,
Emanuel Varga
0
Mike Baldini
Top achievements
Rank 1
answered on 27 Aug 2010, 06:28 PM
The Conditional Formatting for the cells worked great for actually setting the colors, however in the CellFormatting event i was able to create a gradient in the cells. The conditional formatting makes the entire background of the cell a solid color. Is there any way to keep the gradients I had under the CellFormatting event of the radgridview?
0
Svett
Telerik team
answered on 01 Sep 2010, 09:19 AM
Hi Mike Baldini,

You cannot define gradient background through the Conditional Formatting functionality. We will consider it as feature request. I suppose that the issue is caused by the fact that you do not reset the value of the properties that you are changing. When the grid reuses a cell you should reset its properties to the default values.

You can use the following code snippet as a sample:
private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    GridViewDataColumn dataColumn = e.CellElement.ColumnInfo as GridViewDataColumn;
 
    if (dataColumn.Name != "ID")
    {
        return;
    }
 
    int value = Convert.ToInt32(e.CellElement.Value);
 
 
    if (value % 2 == 0)
    {
        e.CellElement.NumberOfColors = 2;
        e.CellElement.BackColor = Color.Red;
        e.CellElement.BackColor2 = Color.Green;
        e.CellElement.ForeColor = Color.Lime;
        e.CellElement.GradientStyle = GradientStyles.Linear;
        e.CellElement.DrawFill = true;
    }
    else
    {
        e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.BackColor2Property, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
    }
}

If you still experience the issue, please open a new support ticket and attach a sample project where the issue can be reproduced. This will allow me to assist you further.

Sincerely yours,
Svett
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
sanjeev
Top achievements
Rank 1
answered on 17 May 2011, 08:18 AM
Hello
 I want to change the color of particular column.suppose there are 6 columns and i want to change the color of 2 columns on grid load.
I am loading all the 6 column from database..If possible please provide me some code..


Please Suggest..

Thanks
0
sanjeev
Top achievements
Rank 1
answered on 17 May 2011, 08:51 AM
Hello i got the solution for changing the background color of a particular column
i Did Following
private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
   
    if (e.CellElement.ColumnIndex == 0)
    {
        e.CellElement.NumberOfColors = 2;
        e.CellElement.BackColor = Color.Red;
        e.CellElement.BackColor2 = Color.Green;
        e.CellElement.ForeColor = Color.Lime;
        e.CellElement.GradientStyle = GradientStyles.Linear;
        e.CellElement.DrawFill = true;
    }
Tags
GridView
Asked by
Mike Baldini
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Mike Baldini
Top achievements
Rank 1
Svett
Telerik team
sanjeev
Top achievements
Rank 1
Share this question
or