473,657 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

change datagrid column color

How do I set the color of a particular column in my C# winforms datagrid?

Thanks,
Dave

Nov 15 '05 #1
4 12813
It's really easy if you're using Visual Studio. Right click on the DataGrid and go to the property builder. Click on the format tab and under columns in the tree view you can set all properties for different columns or all at once.
Nov 15 '05 #2
I should clarify, It's a windows forms app, visual studio 2002, populated in
the code below.

I don't know what the syntax is for setting the column color of a specific
column (for example, when i=2, I need to column color to be gray). I don't
think the property builder is available in this situation (is that just for
web apps?).

Thanks,
Dave
DataGridTableSt yle tableStyle = new DataGridTableSt yle();
tableStyle.Mapp ingName = "Defs";
DataTable dt = dsDefs.Tables["Defs"];
for(int i = 0; i < dt.Columns.Coun t; ++i)
{
DataGridTextBox Column TextCol = new DataGridTextBox Column();
TextCol.Mapping Name = dt.Columns[i].ColumnName;
TextCol.HeaderT ext = dt.Columns[i].ColumnName;
tableStyle.Grid ColumnStyles.Ad d(TextCol);
}
dataGrid1.Table Styles.Clear();
dataGrid1.Table Styles.Add(tabl eStyle);
dataGrid1.DataS ource = dt;
"Dave Petersen" <da************ *******@carlson .com> wrote in message
news:uX******** ******@TK2MSFTN GP12.phx.gbl...
How do I set the color of a specific column in my C# winforms datagrid?

Thanks,
Dave

Nov 15 '05 #3
Dave,

You should create a derived DataGridColumnS tyle (most likely inherited from
the DataGridTextBox Column) and override its Pain method to alter the
background color depending on the cell value.

MSDN has a very good article on customizing the DataGrid, try searching for
the "Customizin g DataGrid" keywords.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Dave Petersen" <da************ *******@carlson .com> wrote in message
news:%2******** *******@TK2MSFT NGP12.phx.gbl.. .
I should clarify, It's a windows forms app, visual studio 2002, populated in the code below.

I don't know what the syntax is for setting the column color of a specific
column (for example, when i=2, I need to column color to be gray). I don't think the property builder is available in this situation (is that just for web apps?).

Thanks,
Dave
DataGridTableSt yle tableStyle = new DataGridTableSt yle();
tableStyle.Mapp ingName = "Defs";
DataTable dt = dsDefs.Tables["Defs"];
for(int i = 0; i < dt.Columns.Coun t; ++i)
{
DataGridTextBox Column TextCol = new DataGridTextBox Column();
TextCol.Mapping Name = dt.Columns[i].ColumnName;
TextCol.HeaderT ext = dt.Columns[i].ColumnName;
tableStyle.Grid ColumnStyles.Ad d(TextCol);
}
dataGrid1.Table Styles.Clear();
dataGrid1.Table Styles.Add(tabl eStyle);
dataGrid1.DataS ource = dt;
"Dave Petersen" <da************ *******@carlson .com> wrote in message
news:uX******** ******@TK2MSFTN GP12.phx.gbl...
How do I set the color of a specific column in my C# winforms datagrid?

Thanks,
Dave



Nov 15 '05 #4
Thanks Dmitriy,
I will give it a try.
Dave

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.c om> wrote
in message news:#e******** ******@TK2MSFTN GP12.phx.gbl...
Dave,

You should create a derived DataGridColumnS tyle (most likely inherited from the DataGridTextBox Column) and override its Pain method to alter the
background color depending on the cell value.

MSDN has a very good article on customizing the DataGrid, try searching for the "Customizin g DataGrid" keywords.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Dave Petersen" <da************ *******@carlson .com> wrote in message
news:%2******** *******@TK2MSFT NGP12.phx.gbl.. .
I should clarify, It's a windows forms app, visual studio 2002, populated
in
the code below.

I don't know what the syntax is for setting the column color of a

specific column (for example, when i=2, I need to column color to be gray). I

don't
think the property builder is available in this situation (is that just

for
web apps?).

Thanks,
Dave
DataGridTableSt yle tableStyle = new DataGridTableSt yle();
tableStyle.Mapp ingName = "Defs";
DataTable dt = dsDefs.Tables["Defs"];
for(int i = 0; i < dt.Columns.Coun t; ++i)
{
DataGridTextBox Column TextCol = new DataGridTextBox Column();
TextCol.Mapping Name = dt.Columns[i].ColumnName;
TextCol.HeaderT ext = dt.Columns[i].ColumnName;
tableStyle.Grid ColumnStyles.Ad d(TextCol);
}
dataGrid1.Table Styles.Clear();
dataGrid1.Table Styles.Add(tabl eStyle);
dataGrid1.DataS ource = dt;
"Dave Petersen" <da************ *******@carlson .com> wrote in message
news:uX******** ******@TK2MSFTN GP12.phx.gbl...
How do I set the color of a specific column in my C# winforms datagrid?
Thanks,
Dave


Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6316
by: Job Lot | last post by:
How can I conditionally change the color of a row in DataGrid? I have a column named “MonthClosed” in my DataGrid. If the value of in this column of a row is True then I want to set the color of entire row to Green otherwise as Blue. Thanx
6
11532
by: Barney Nicholls | last post by:
I have a datagrid and want to change the background color of the end column to denote that it is the only column that is not read only. I still want this column to be editable. I've found examples that let you change the background color of the cell but it but the cell no longer appears to be editable.
0
2646
by: Amber | last post by:
There are times when you will need to highlight or otherwise modify the contents of a particular DataGrid row-column value based upon the value in the column. In this example we will select the CompanyName, ContactName, and ContactTitle columns from the Customers table in the Northwind database. Whenever the value of ContactTitle equals "Owner" we will change the font color to red. We can do this by using an ItemTemplate where we have...
0
1501
by: Dan Hartshorn | last post by:
VS.NET 2003, C#, Windows Server 2003. I have a datagrid and I want the last column to be either an EditCommandColumn or a template column, depending on a value I have. The value changes for each row. So some rows will have an EditCommandColumn, others just a text message in the last column. Is this possible? I am already using the ItemDataBound method to perform some function on each row, but I can't seem to change the datagrid's column...
4
2032
by: IGotYourDotNet | last post by:
on my web app, i need to display data from a table in a grid. (i know how to do that). The grid will be populated with data depending on what the user selects in a drop down box. My question is instead of having 20 grids can i use only 1 and change the data that should be displayed and the headers via code, or do I need a seperate grid per choice in the drop down box.
2
6196
by: gh0st54 | last post by:
hi still working on changing my data grid headers using resource files the thing is i always gat a count of 0 for my datagrid columns count, when will i get the columns count and when should i change the headers ?? thanks private void Page_Load(object sender, System.EventArgs e)
5
4237
by: HS1 | last post by:
Hello I have a datagrid to show data for a database table using "seclect * from tablename" The datagrid works OK. However, I want to change the name of the fields in the database to other name in DataGrid. For example, a field name in database is "FName" will be shown in dataGrid as "First Name". Could you please tell me how to do that
4
10693
by: Roger | last post by:
I have a datagrid and would like to know what even fires when a cell is changed? I want to know when the user changes a cell and moves to the next. I have some code that needs to be done to make sure entry is valid? Thanks, Rog
2
2122
by: Billy | last post by:
Change DataGrid EditControl On Data Value Hi, I have a datagrid, and on editing, I want to change the control in the third colunm based on the value of the first column. The value in the first column can only be either "Text" or "Image", which is selcted in a dropdown list.
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8606
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4159
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2732
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1622
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.