change datagrid column color

D

Dave Petersen

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

Thanks,
Dave
 
G

Guest

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.
 
D

Dave Petersen

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.ColumnName;
TextCol.HeaderText = dt.Columns.ColumnName;
tableStyle.GridColumnStyles.Add(TextCol);
}
dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(tableStyle);
dataGrid1.DataSource = dt;
 
D

Dmitriy Lapshin [C# / .NET MVP]

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 said:
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.ColumnName;
TextCol.HeaderText = dt.Columns.ColumnName;
tableStyle.GridColumnStyles.Add(TextCol);
}
dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(tableStyle);
dataGrid1.DataSource = dt;


Dave Petersen said:
How do I set the color of a specific column in my C# winforms datagrid?

Thanks,
Dave
 
D

Dave Petersen

Thanks Dmitriy,
I will give it a try.
Dave

Dmitriy Lapshin said:
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 said:
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.ColumnName;
TextCol.HeaderText = dt.Columns.ColumnName;
tableStyle.GridColumnStyles.Add(TextCol);
}
dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(tableStyle);
dataGrid1.DataSource = dt;


Dave Petersen said:
How do I set the color of a specific column in my C# winforms datagrid?

Thanks,
Dave

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top