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

Gridview color for selected row.

3 Answers 514 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Karthick
Top achievements
Rank 1
Karthick asked on 21 Dec 2011, 01:03 AM
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?

3 Answers, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 22 Dec 2011, 12:56 PM
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.
0
Sean
Top achievements
Rank 1
answered on 22 Dec 2011, 07:13 PM
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

0
Svett
Telerik team
answered on 27 Dec 2011, 02:30 PM
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.
Tags
GridView
Asked by
Karthick
Top achievements
Rank 1
Answers by
Svett
Telerik team
Sean
Top achievements
Rank 1
Share this question
or