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

Cell color scrolling

7 Answers 235 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 1
Matt asked on 20 Feb 2011, 06:31 AM
Hi all,

I have need for a cell background color to change when the data changes in that cell..  Currently I have it working in the ValueChanged event and it works fine, but when I scroll, the cell color doesn't scroll with the rows..  I undersgtand this is due to the visual elements and that I should set the color in the RowFormatting event, however, I have no idea what the value is, and can not do "conditional formatting"...  is there a way to know, in RowFormatting, that the cell has been changed?
Thanks,

Matt

My code to do this in CellValueChanged is:
void gvMain_CellValueChanged(object sender, GridViewCellEventArgs e)
{
    GridDataCellElement ce = (GridDataCellElement)sender;
    ce.BackColor = Color.LightPink;
    ce.BackColor2 = Color.White;
    ce.NumberOfColors = 2;
    ce.DrawFill = true;
    ce.GradientStyle = Telerik.WinControls.GradientStyles.Linear;
    _dataDirty = true;
    btnSave.Visible = true;
}

7 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 20 Feb 2011, 11:14 AM
Hello,

In order to colour the cells you'll need to use the CellFormatting event. The reason, as you said that the cells are all being coloured when scrolling is because of the UI virtualization system that the grid uses, essentially re-using cells as data is scrolled into them.

In the cell formatting event you will need to also reset the cell colour value. All the information on cell formatting can be found here in the documentation.

You can cause formatting to fire at any time by calling the RowInvalidate method on any row. For example
Me.RadGridView1.CurrentRow.InvalidateRow()

Hope you find this helpful,
Richard
0
Richard Slade
Top achievements
Rank 2
answered on 20 Feb 2011, 12:49 PM
Hi Matt,

I thought it would be of benefit if I included an exmaple for you of using CellFormatting together with CellValueChanged.

private void radGridView1_CellValueChanged(object sender, GridViewCellEventArgs e)
{
    GridDataCellElement ce = (GridDataCellElement)sender;
    ce.Tag = "modified";
}
private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.CellElement.Tag != null)
    {
        if (e.CellElement.Tag.ToString() == "modified")
        {
            e.CellElement.BackColor = Color.LightPink;
            e.CellElement.NumberOfColors = 1;
            e.CellElement.DrawFill = true;
        }
        else
        {
            e.CellElement.ResetValue(LightVisualElement.BackColorProperty);
            e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty);
            e.CellElement.ResetValue(LightVisualElement.DrawFillProperty);
        }
    }
    else
    {
        e.CellElement.ResetValue(LightVisualElement.BackColorProperty);
        e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty);
        e.CellElement.ResetValue(LightVisualElement.DrawFillProperty);
    }
}

Hope you find this helpful
Richard
0
Richard Slade
Top achievements
Rank 2
answered on 22 Feb 2011, 08:50 AM
Hello,

Did this help? If so please remember to mark as answer. If you need futher information, do let me know
Thanks
Richard
0
Matt
Top achievements
Rank 1
answered on 24 Feb 2011, 10:12 PM
Hi Richard,

Sorry, just getting a chance to try this out.. looks like it still doing the same thing...  I set the tag in CellValueChanged, and the format in CellFormatting, as per your code, but the color still stays put while the rows scroll..

Here's the full code for those events:
void gvMain_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.CellElement.Tag != null)
    {
        if (e.CellElement.Tag.ToString() == "modified")
        {
            e.CellElement.BackColor = Color.LightPink;
            e.CellElement.NumberOfColors = 1;
            e.CellElement.DrawFill = true;
        }
        else
        {
            e.CellElement.ResetValue(LightVisualElement.BackColorProperty);
            e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty);
            e.CellElement.ResetValue(LightVisualElement.DrawFillProperty);
        }
    }
    else
    {
        e.CellElement.ResetValue(LightVisualElement.BackColorProperty);
        e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty);
        e.CellElement.ResetValue(LightVisualElement.DrawFillProperty);
    }
}
 
void gvMain_CellValueChanged(object sender, GridViewCellEventArgs e)
{
    GridDataCellElement ce = (GridDataCellElement)sender;
    ce.Tag = "modified";
 
    _dataDirty = true;  // Page switch to cause the dataset to update grid changes
    btnSave.Visible = true;
    btnCancel.Visible = true;
}

Thanks!

Matt
0
Richard Slade
Top achievements
Rank 2
answered on 25 Feb 2011, 12:29 AM
Hello,

My apologies. I have given you some wrong information here. This wouldn't work because tagging the cell will produce the same result as the cell element is re-used, and therefore as you scroll the cell elements tag is still "modified" and it colours the cell. I will find another way for you and let you know as soon as I can.
Richard
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 25 Feb 2011, 12:54 AM
Hi Matt,

Here you go, this is what I should have given you. This uses the CellInfo to tag a value rather than the cell element.
Please can you try this.

void radGridView1_CellValueChanged(object sender, GridViewCellEventArgs e)
{
    this.radGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag = "Modified";
    this.radGridView1.CurrentRow.InvalidateRow();
}

void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.RowIndex > -1)
    
        if (this.radGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag != null)
        {
            if (this.radGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag.ToString() == "Modified")
            {
                e.CellElement.BackColor = Color.LightPink;
                e.CellElement.NumberOfColors = 1;
                e.CellElement.DrawFill = true;
            }
            else
            {
                e.CellElement.ResetValue(LightVisualElement.BackColorProperty);
                e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty);
                e.CellElement.ResetValue(LightVisualElement.DrawFillProperty);
            }
        }
        else
        {
            e.CellElement.ResetValue(LightVisualElement.BackColorProperty);
            e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty);
            e.CellElement.ResetValue(LightVisualElement.DrawFillProperty);
        }             
    }
}

Hope you find that helps
Richard
0
Matt
Top achievements
Rank 1
answered on 25 Feb 2011, 03:30 AM
Thanks Richard, that works perfect!

Matt
Tags
GridView
Asked by
Matt
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Matt
Top achievements
Rank 1
Share this question
or