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

Custom style in some columns

3 Answers 309 Views
GridView
This is a migrated thread and some comments may be shown as answers.
konrad
Top achievements
Rank 1
konrad asked on 11 Dec 2012, 11:57 AM
Hi,

How to apply (by c# code) custom style (fore color, back color, font etc..) to only specific column, so style will affect all data in that column? I also want change style for individual column header cell and summary cell.
I'm only interested in the change of these properties in some of the columns, not all.

3 Answers, 1 is accepted

Sort by
0
Accepted
Plamen
Telerik team
answered on 14 Dec 2012, 09:33 AM
Hello Dominik,

Thank you for writing.

To customize the appearance of the cells in RadGridView you should use the formatting events: http://www.telerik.com/help/winforms/gridview-cells-formatting-cells.html. As stated in the article one should use the CellFormatting event to style the data cells and the ViewCellFormatting event for all cells.

1. To style your data cells you can use the following code:
void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.CellElement.ColumnInfo.Name == "Status")
    {
        e.CellElement.ForeColor = Color.Red;
        e.CellElement.BackColor = Color.Yellow;
        e.CellElement.GradientStyle = GradientStyles.Solid;
        e.CellElement.DrawFill = true;
    }
    else
    {
        e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
    }
}

2. To style a specific header cell you need to use the ViewCellFormatting event. Here is an example:
void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.CellElement.ColumnInfo.Name == "Status" && e.CellElement is GridHeaderCellElement)
    {
        e.CellElement.BackColor = Color.Yellow;
        e.CellElement.GradientStyle = GradientStyles.Solid;
        e.CellElement.DrawFill = true;
    }
    else
    {
        e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
    }
}

3. As to the summary cell, you will also need to use the ViewCellFormatting event handler:
void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.CellElement.ColumnInfo.Name == "Status" && e.CellElement is GridSummaryCellElement)
    {
        e.CellElement.BackColor = Color.Red;
        e.CellElement.GradientStyle = GradientStyles.Solid;
        e.CellElement.DrawFill = true;
    }
    else
    {
        e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
    }
}

Please take a look at the articles mentioned above for additional information and examples.

I hope this helps. Let me know if you have additional questions.

Kind regards,
Plamen
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
0
konrad
Top achievements
Rank 1
answered on 19 Dec 2012, 08:22 AM
Thank you for your answer!

For GridHeaderCellElement - work like a charm.
For GridCellElement work only if we have the data in the table. It is obvious.

The problem is that I need to read and modify the formatting of a cell in an empty grid. In other words, I need to set something like default format for the data which will appear in the future.
Basically I am looking for property like DefaultCellStyle in standard .NET DataGridView.
I know that I could temporarily add one row to perform this function but it is not a elegant solution.


0
Plamen
Telerik team
answered on 21 Dec 2012, 11:54 AM
Hello Dominik,

Thank you for writing back.

In my previous post I have missed the summary cell question. This is why I am going to edit it to add the omitted information and update the data cells sample.

Kind regards,
Plamen
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
Tags
GridView
Asked by
konrad
Top achievements
Rank 1
Answers by
Plamen
Telerik team
konrad
Top achievements
Rank 1
Share this question
or