设置DataGridView.ColumnHeaderDefaultCellStyle的BackColor属性会发现没有效果。这是因为在启动了可视样式的时候,BackColor和ForeColor的值会被忽略。 要解决很简单:datagridview1.EnableHeadersVisualStyles = false; 获取或设置一个值,该值指示在对应用程序启用了可视样式的情况下,行标题和列标题是否使用用户当前主题的可视样式。
//dataGridView.AutoResizeColumnHeadersHeight();
//dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
//dataGridView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
//dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
//dataGridView.ReadOnly = true;
//dataGridView.AllowUserToAddRows = false;
//dataGridView.BackgroundColor = Color.White; //属性
dataGridView.EnableHeadersVisualStyles = false; dataGridView.AllowUserToAddRows = false;//禁止用户添加行 //dataGridView.AllowUserToResizeColumns = false;//禁止用户调整列大小 dataGridView.AllowUserToResizeRows = false;//禁止用户调整行大小 dataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;//如何选择单元格 dataGridView.RowHeadersVisible = false; //是否显示行头 //dataGridView.ColumnHeadersVisible = false; //是否显示列头 dataGridView.AutoGenerateColumns = false; //是否自动创建列 //dataGridView.MultiSelect = false; //是否可以选择多行
//样式 dataGridView.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Sunken;//列头边框样式
dataGridView.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Sunken;//行头边框样式 DataGridViewCellStyle dataGridViewCellStyle1 = dataGridView.AlternatingRowsDefaultCellStyle; //奇数行单元格样式
//DataGridViewCellStyle dataGridViewCellStyle3 = dataGridView.RowsDefaultCellStyle; //行单元格默认样式
DataGridViewCellStyle dataGridViewCellStyle3 = dataGridView.RowHeadersDefaultCellStyle; //行头单元格样式
DataGridViewCellStyle dataGridViewCellStyle2 = dataGridView.ColumnHeadersDefaultCellStyle; //列头单元格样式 //奇数行单元格样式设置
dataGridViewCellStyle1.BackColor = System.Drawing.Color.LightYellow; ////行头单元格样式设置
//dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;//内容位置
//dataGridViewCellStyle3.BackColor = System.Drawing.Color.DarkRed;//背景
//dataGridViewCellStyle3.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));//字体
//dataGridViewCellStyle3.ForeColor = System.Drawing.Color.White;//字体颜色
//dataGridViewCellStyle3.SelectionBackColor = System.Drawing.SystemColors.Highlight;//选择时背景
//dataGridViewCellStyle3.SelectionForeColor = System.Drawing.SystemColors.HighlightText;//选择时字体颜色
//dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.True;//单元格内容太长时显示格式 //列头单元格样式设置
dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
dataGridViewCellStyle2.BackColor = System.Drawing.Color.DarkRed;
dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
dataGridViewCellStyle2.ForeColor = System.Drawing.Color.White;
dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
for (int i = 0; i < dataGridView.ColumnCount; i++)
{
dataGridView.Columns.Width = (dataGridView.Width /*- dataGridView.RowHeadersWidth*/ - 15) / dataGridView.ColumnCount;
}
|