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

Change background of selected row

3 Answers 547 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Terje
Top achievements
Rank 2
Terje asked on 12 May 2016, 01:12 PM

Hi!

I want to change the background color of the selected row, i also want some headings to have a different color. So far so good..When I click on the "Add new row" I don't want the selected row to go back to the default color, but stay the way it was. Any suggestions on how to accomplish that? This is my code so far:

private void rgv_RowFormatting(object sender, RowFormattingEventArgs e)
{
    if (e.RowElement is GridDataRowElement)
    {
        if (e.RowElement.RowInfo.IsCurrent) //Selected row
        {   
            e.RowElement.BackColor = Color.PaleVioletRed;
            e.RowElement.GradientStyle = GradientStyles.Solid;
            e.RowElement.DrawFill = true;
        }
        else if ((int)e.RowElement.RowInfo.Cells["Heading"].Value == 1) //Color on heading
        {
            e.RowElement.BackColor = Color.GreenYellow;
            e.RowElement.GradientStyle = GradientStyles.Solid;
            e.RowElement.DrawFill = true;
        }
        else //Reset color
        {
            e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
            e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
            e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
        }
    }
}

3 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 17 May 2016, 05:30 AM
Hello Terje,

Thank you for writing.

According to the provided code snippet, you are not supposed to affect the new row at all. Firstly, it is necessary to subscribe to the ViewRowFormatting event. In addition, you should apply the desired style for the GridNewRowElement
private void radGridView1_ViewRowFormatting(object sender, Telerik.WinControls.UI.RowFormattingEventArgs e)
{
    if (e.RowElement is GridDataRowElement || e.RowElement is GridNewRowElement)
    {
        if (e.RowElement.RowInfo.IsCurrent) //Selected row
        {
            e.RowElement.BackColor = Color.PaleVioletRed;
            e.RowElement.GradientStyle = GradientStyles.Solid;
            e.RowElement.DrawFill = true;
        }
        else if ((e.RowElement.RowInfo.Cells["ContactName"].Value+"").Contains("e")) //Color on heading
        {
            e.RowElement.BackColor = Color.GreenYellow;
            e.RowElement.GradientStyle = GradientStyles.Solid;
            e.RowElement.DrawFill = true;
        }
        else //Reset color
        {
            e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
            e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
            e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
        }
    }
}

I hope this information helps. Should you have further questions I would be glad to help.

 Regards,
Dess
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Terje
Top achievements
Rank 2
answered on 17 May 2016, 09:21 PM

Hi!

 

Thank's! I also got the answer from this thread: www.telerik.com/forums/how-to-hide-quot-click-here-to-add-new-row-quot-option-on-radgridview

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 20 May 2016, 08:47 AM
Hello Terje,

Thank you for writing back. 

Use the RowFormatting event to apply custom formatting to RadGridView's data rows. To customize the non-data rows (header row, new row, filtering row, etc) of RadGridView, you need to handle the ViewRowFormatting event. Additional information is available here: http://docs.telerik.com/devtools/winforms/gridview/rows/formatting-rows

I hope this information helps. If you have any additional questions, please let me know.

Regards,
Dess
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
GridView
Asked by
Terje
Top achievements
Rank 2
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Terje
Top achievements
Rank 2
Share this question
or