D
Dave Petersen
How do I set the color of a particular column in my C# winforms datagrid?
Thanks,
Dave
Thanks,
Dave
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
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
populatedDave Petersen said:I should clarify, It's a windows forms app, visual studio 2002,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