Gridview color for selected row.

4 posts, 0 answers
  1. Karthick
    Karthick avatar
    2 posts
    Member since:
    Oct 2006

    Posted 20 Dec 2011 Link to this post

    In RadGridView, I am trying the following:

    1.  When the row is clicked/selected, show 1 color. - Telerik grid had a default color, when i went to grid's row formatting  event:

     if (e.RowElement.IsSelected)
                {
                    e.RowElement.GradientStyle = GradientStyles.Solid;
                    e.RowElement.BackColor = System.Drawing.Color.Red;
                    e.RowElement.DrawFill = true;
                }

    The issue is first row is selected as default when the grid is bound, when i click any rows , the first row that is already selected still shows as selected (although multiselect is false for the grid.), how do i not select first row, if any new row is clicked?

    2. Also when i select/click on the row, always the first cell is highlighted, how do i disable that?
  2. Svett
    Admin
    Svett avatar
    728 posts

    Posted 22 Dec 2011 Link to this post

    Hi Karthick,

    You should reset the visual styles that you have modified if the row is not selected:

    private void OnRowFormatting(object sender, Telerik.WinControls.UI.RowFormattingEventArgs e)
    {
        if (e.RowElement.IsSelected)
        {
            e.RowElement.GradientStyle = GradientStyles.Solid;
            e.RowElement.BackColor = System.Drawing.Color.Red;
            e.RowElement.DrawFill = true;
        }
        else
        {
            e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
            e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
            e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
        }
    }

    Regarding your second questions, you can use the CellFormatting event to achieve it:
    private void OnCellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.CellElement.IsCurrent)
        {
            e.CellElement.DrawFill = false;
        }
        else
        {
            e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
        }
    }

    I hope this helps.

    Greetings,
    Svett
    the Telerik team

    Q3’11
    of RadControls for WinForms is available for download (see what's new). Get it today.
  3. Sean
    Sean avatar
    1 posts
    Member since:
    Aug 2012

    Posted 22 Dec 2011 Link to this post

    Thanks Svett, it helped. But there is something which the OnCellFormatting didn't do.

    Please refer(download) this small screen from below link:

    Outer color still shown


    Outer color is still shown in orange & there is a yellow color over the row selected, I need to select only one color when the row is selected.

    if (e.RowElement.IsSelected)
        {
            e.RowElement.GradientStyle = GradientStyles.Solid;
            e.RowElement.BackColor = System.Drawing.Color.Red;
            e.RowElement.DrawFill = true;
        }

    Ie. Only Red color should be there & not any additional color.


    Thanks in advance,
    Karthick

  4. Svett
    Admin
    Svett avatar
    728 posts

    Posted 27 Dec 2011 Link to this post

    Hi Sean,

    Thank you for writing back.

    You should use the ViewCellFormatting instead to achieve the desired look. Here is the modified version of my previous code snippet:
    this.radGridView1.RowFormatting += this.OnRowFormatting;
    this.radGridView1.ViewCellFormatting += this.OnViewCellFormatting;

    private void OnViewCellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.CellElement.RowElement.IsCurrent || e.CellElement.RowElement.IsSelected)
        {
            e.CellElement.BorderColor = Color.Red;
            e.CellElement.BorderBoxStyle = BorderBoxStyle.SingleBorder;
            e.CellElement.BorderGradientStyle = GradientStyles.Solid;
            e.CellElement.DrawFill = false;
        }
        else
        {
            e.CellElement.ResetValue(LightVisualElement.BorderColorProperty, ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.BorderBoxStyleProperty, ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.BorderGradientStyleProperty, ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
        }
    }
     
    private void OnRowFormatting(object sender, Telerik.WinControls.UI.RowFormattingEventArgs e)
    {
        if (e.RowElement.IsSelected || e.RowElement.IsCurrent)
        {
            e.RowElement.GradientStyle = GradientStyles.Solid;
            e.RowElement.BackColor = System.Drawing.Color.Red;
        }
        else
        {
            e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
            e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
            e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
            e.RowElement.ResetValue(LightVisualElement.DrawBorderProperty, ValueResetFlags.Local);
        }
    }

    I hope that you find this information useful. Should you have any other questions, do not hesitate to contact us.

    Greetings,
    Svett
    the Telerik teamQ3’11 of RadControls for WinForms is available for download (see what's new). Get it today.
Back to Top