Click here to Skip to main content
15,917,061 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I would like to know how to change the backgroundColor of all DataGridViews controls in my project programatically.
Posted
Updated 8-Dec-11 20:29pm
v3
Comments
Sergey Alexandrovich Kryukov 9-Dec-11 2:07am    
Run-time or design time?
--SA

Hi,

Advise you to
Create a skin 'A file used to define an ASP.NET theme'
and create a CssClass...

Example: file Name 'Skinless' which contents

XML
<asp:GridView BorderWidth="1" BorderColor="White" PageSize="10" AllowPaging="True" Width="100%" CellPadding="5"  runat="server" AutoGenerateColumns="false">
    <RowStyle CssClass="clsGridRow" HorizontalAlign="Left" />
    <EmptyDataRowStyle CssClass="clsGridEmptyRow" />
    <PagerStyle HorizontalAlign="Center" CssClass="clsGridPager" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle CssClass="clsGridHdr" ForeColor="White" />
    <FooterStyle CssClass="clsGridFtr"/>
    <EditRowStyle BackColor="#D1DDF1" />
    <AlternatingRowStyle CssClass="clsGridAlternate"/>
</asp:GridView>


Then in your client code page

Example code:

C#
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" EnableTheming="true" Theme="Skinless" Title = " My Login Page"%>



Hope this could help...

Regards,
 
Share this answer
 
v2
Hello,
if you want to change the DataGridVew BackgroundColor for all
DataGridViews in your project programatically use :

<pre lang="c#">
dataGridView1.BackgroundColor = Color.Red;
dataGridView2.BackgroundColor = Color.Red;
...
...
...
dataGridViewN.BackgroundColor = Color.Red;
</pre>

You can do this in all Forms that you use
in project that contains the DataGridViews.

If you use more than one Form , for example Form1 , Form2 ...
that as well contains DataGridViews and you want to change BackgroundColor from
the MainForm , first you have to declare
Modifiers of those DataGridViews as public
(I am using IDE SharpDevelop so it is in the properties under Design/Modifiers)and then you can use :

<pre lang="c#">
Form1 f1 = new Form1();
Form2 f2 = new Form2();
</pre>


<pre lang="c#">
f1.dataGridView1.BackgroundColor = Color.Red;
f1.dataGridView2.BackgroundColor = Color.Red;
...
...
...
f1.dataGridViewN.BackgroundColor = Color.Red;

f2.dataGridView1.BackgroundColor = Color.Red;
f2.dataGridView2.BackgroundColor = Color.Red;
...
...
...
f2.dataGridViewN.BackgroundColor = Color.Red;
</pre>

This way you can write a routine that you will call from different places in the project and thus change the background color from one place.
 
Share this answer
 
Comments
Perić Željko 9-Dec-11 12:39pm    
<Pre tags> are still enigma for me ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900