Hi,
I must be missing something obvious but how do I set the background colour of a Gridview control.
I can set the background colour of a row or cell but I cannot see how to set the colour of "White space" in other words my grid takes up have the screen but it might only contain 2 rows, which I can colour, but the bank space is always white.
Any ideas how I can change this??
Cheers
Shaun.
7 Answers, 1 is accepted
Thank you for writing.
You can change the background color of RadGridView through its table element. For example :
this
.radGridView1.TableElement.BackColor = Color.Red;
Please note that by default cells and rows back colors are transparent, so if you change the color of table element, you should use RowFormatting (or change the design by a theme) event to set the row's desired color.
I hope this helps.
All the best,
Anton
the Telerik team
I'm trying this approach, but the background is still white. There are only three rows, but the rest of the grid control is white. Here is the code:
this.radGridView1.TableElement.GradientStyle = GradientStyles.Solid;
this.radGridView1.TableElement.GridViewElement.BackColor = Color.LightGray;
this.radGridView1.TableElement.BackColor = Color.LightGray;
this.radGridView1.TableElement.BackColor2 = Color.LightGray;
this.radGridView1.TableElement.BackColor3 = Color.LightGray;
this.radGridView1.TableElement.BackColor4 = Color.LightGray;
Hello, David,
I have checked it on my side and confirm that setting the TableElement.BackColor does change the color to LightGray.
Maybe there is something different on your side. Can you please specify which is the exact theme that you use?
Looking forward to your reply.
Thanks for the response. The data is retrieved dynamically. I was setting the BackColor before I bound the grid to the data. I have to set the TableElement.BackColor every time I bind the data.
I'm not really using a theme. But, I'm using EnableAlternatingRowColor = true. And now I have these issues below. I have tried to use the radGridView1_RowFormatting event, but it seems to ignore the formatting set in that event:
1) The orange selected row AND cell should be Color.FromArgb(234, 253, 172).
2) The non-alternate rows should have a white background.
How do I set the backcolor of the selected row, and then the backcolor of the non-selected, non-alternate row?
Thanks,
David
Hello, David,
Upon reading your latest reply it seems that you managed to get the things done according to your needs by using the ViewCellFormatting event. I am really glad to hear this event is helpful to you. Using formatting events is the right approach when trying to customize colors and look in RadGridView.
If you have any other questions, do not hesitate to contact us.
How would you format the 'nonclient' area of a subtable?
I create my table in code, then create a subtable template and add the relationship.
thanks
Thank you for writing.
I am not sure what you mean with "'nonclient' area of a subtable", but I suppose that you want to change the color of the details view of hierarchical grid. If so, you can do this by subscribing to ViewCellFormatting event of the grid. For example:
void
radGridView1_ViewCellFormatting(
object
sender, CellFormattingEventArgs e)
{
if
(e.CellElement
is
GridDetailViewCellElement)
{
e.CellElement.BackColor = Color.Red;
e.CellElement.BackColor2 = Color.Red;
}
}
If this is now what you are trying to achieve, please provide me with some screen shots where the desired area is marked, so I can provide you with adequate support.
Let me know if you have additional questions.
All the best,
Anton
the Telerik team
The area I'm trying to shade is pointed to by the arrows in the attached image. The dark gray area of the main table shades perfectly with a
m_Index.TableElement.BackColor = Color.FromArgb(64, 64, 64); but i can't seem to find anywhere to set the related subtable color the same.
And your response has led me to a second question, for the gradient shaded cells i've had to use CustomizeFill = true on each GridViewCellInfo where I want a gradient shade. As whenever i do code similar to what you provided in ViewCellFormatting or in CellFormatting event it looks perfect until you move the mouse over the cell and it reverts to a flat shade. Is there much cost for having customised cell shading (I need some customised as they animate) so it seemed the sensible thing to do to make all of them shade using the same routines.
Thanks in advanced
tony
Thank you for writing back.
The element the arrows point at is the table element of the grid details row cell. You can use the ViewCellFormatting event to change this color. Here is a code snippet which demonstrates how:
private
void
radGridView1_ViewCellFormatting(
object
sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
{
GridDetailViewCellElement cell = e.CellElement
as
GridDetailViewCellElement;
if
(cell !=
null
)
{
cell.ChildTableElement.BackColor = Color.Gray;
}
}
I am not sure I fully understand your second question. The best way to customize cell elements is through the CellFormatting/ViewCellFormating events. There you have access to the actual element that will be painted and you can modify it. The formatting events are also fired for only the visible cells as they are the only elements created (thanks to the grid UI virtualization) so they are not slowing down the grid. If this does not answer your questions, I would kindly ask you to provide some more information on what you want to achieve.
I hope this will be useful. Should you have further questions, do not hesitate to write back.
Regards,
Ivan Petrov
the Telerik team
I'll try to think of a better way of explaining the other problem.
Tony
I am looking forward to your reply when you have the time to prepare it.
Greetings,
Ivan Petrov
the Telerik team