473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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 12789
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
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = "Defs";
DataTable dt = dsDefs.Tables["Defs"];
for(int i = 0; i < dt.Columns.Count; ++i)
{
DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = dt.Columns[i].ColumnName;
TextCol.HeaderText = dt.Columns[i].ColumnName;
tableStyle.GridColumnStyles.Add(TextCol);
}
dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(tableStyle);
dataGrid1.DataSource = dt;
"Dave Petersen" <da*******************@carlson.com> wrote in message
news:uX**************@TK2MSFTNGP12.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 DataGridColumnStyle (most likely inherited from
the DataGridTextBoxColumn) 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 "Customizing 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***************@TK2MSFTNGP12.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
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = "Defs";
DataTable dt = dsDefs.Tables["Defs"];
for(int i = 0; i < dt.Columns.Count; ++i)
{
DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = dt.Columns[i].ColumnName;
TextCol.HeaderText = dt.Columns[i].ColumnName;
tableStyle.GridColumnStyles.Add(TextCol);
}
dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(tableStyle);
dataGrid1.DataSource = dt;
"Dave Petersen" <da*******************@carlson.com> wrote in message
news:uX**************@TK2MSFTNGP12.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.com> wrote
in message news:#e**************@TK2MSFTNGP12.phx.gbl...
Dave,

You should create a derived DataGridColumnStyle (most likely inherited from the DataGridTextBoxColumn) 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 "Customizing 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***************@TK2MSFTNGP12.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
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = "Defs";
DataTable dt = dsDefs.Tables["Defs"];
for(int i = 0; i < dt.Columns.Count; ++i)
{
DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = dt.Columns[i].ColumnName;
TextCol.HeaderText = dt.Columns[i].ColumnName;
tableStyle.GridColumnStyles.Add(TextCol);
}
dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(tableStyle);
dataGrid1.DataSource = dt;
"Dave Petersen" <da*******************@carlson.com> wrote in message
news:uX**************@TK2MSFTNGP12.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
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...
6
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...
0
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...
0
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...
4
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...
2
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...
5
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...
4
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...
2
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.