Change background of selected row

4 posts, 0 answers
  1. Terje
    Terje avatar
    6 posts
    Member since:
    Oct 2013

    Posted 12 May Link to this post

    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);
            }
        }
    }

  2. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 17 May Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Terje
    Terje avatar
    6 posts
    Member since:
    Oct 2013

    Posted 17 May in reply to Dess Link to this post

    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

  5. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 20 May Link to this post

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