关于 DataGridView 行和列的背景色 - 前景色设置
1. 设定 DataGridView 全部单元格的 Style
DataGridView 内所有单元格的 Style 变更, 可以使用 DataGridView 对象的 DefaultCellStyle 属性实现.
- // 包含 Header 所有的单元格的背景色为黄色
- DataGridView1.DefaultCellStyle.BackColor = Color.Yellow;
- // 包含 Header 所有的单元格的前景色为黄色
- DataGridView1.DefaultCellStyle.ForeColor= Color.Yellow; // 前景色设置, 只需要将 BackColor 改为 ForeColor 即可
2.DataGridView.DefaultCellStyle 属性可以对包含 Header 所有单元格的 Style 进行变更设定, 对除 Header 以外所 有单元格的 Style 进行变更, 可以使用 DataGridView.RowsDefaultCellStyle 属性实现
- // Header 以外所有的单元格的背景色为黄色
- DataGridView1.RowsDefaultCellStyle.BackColor = Color.Yellow;
3. 变更某一个单元格的 Style
DataGridViewCell.Style 属性可以对单一的单元格的 Style 进行变更设定.
如下面的例子, 只对 (0, 0) 单元格的背景色设定为粉红色.
- //(0, 0)单元格的背景色为粉色
- DataGridView1[0, 0].Style.BackColor = Color.Pink;
4. 变更被指定的列, 行的单元格的 Style
DataGridViewColumn.DefaultCellStyle 属性, 可以对列的单元格 Style 进行变更设定. DataGridViewRow.DefaultCellStyle 属性, 可以对行的单元格 Style 进行变更设定.
如下面的例子, 第一列的单元格的背景色为淡蓝色, 第一行的单元格的背景色为淡灰色.
- // 索引 0 列的单元格的背景色为淡蓝色
- DataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.Aqua;
- // 索引 0 行的单元格的背景色为淡灰色
- DataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.LightGray;
5. 变更奇数行的单元格 Style
DataGridView.AlternatingRowsDefaultCellStyle 属性, 可以变更 DataGridView 的奇数行的单元格 Style.
如下面的例子, 奇数行的单元格的背景色设定为黄绿色
- // 奇数行的单元格的背景色为黄绿色
- DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.GreenYellow;
6. 变更列 Header, 行 Header 的单元格 Style
列 Header 的单元格 style 的变更, 可以使用, DataGridView.ColumnHeadersDefaultCellStyle 属性实现. 行 Header 的单元格 Style 的变更, 可以使用 DataGridView.RowHeadersDefaultCellStyle 属性实现. 但是, Header 的是左侧的单元格需要通过 DataGridView.TopLeftHeaderCell 属性, 取得的 DataGridViewHeaderCell 对象的单 元格 Style 进行设定.
如下面的例子, 列 Header 的背景色为象牙色, 行 Header 的背景色为橙色.
- // 列 Header 的背景色为象牙色
- DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Ivory;
- // 行 Header 的背景色为橙色
- DataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.Lime;
补充: 每个 Header 单元格的单元格 Style, 可以使用这一些的方法取得, 和一般的单元格一样, 可以使用 Style 属性变更, 简而言之, 就是个可以对每个单元格进行个性化设置.
关于优先顺序
设定单元格 Style 的属性有优先顺序的. 顺序从高到低如下所示.
1). DataGridViewCell.Style
2). DataGridViewRow.DefaultCellStyle
3). DataGridView.AlternatingRowsDefaultCellStyle
4). DataGridView.RowsDefaultCellStyle
5). DataGridViewColumn.DefaultCellStyle
6). DataGridView.DefaultCellStyle
接下来是 Header 的单元格 Style 属性的优先顺序.
1). DataGridViewCell.Style
2). DataGridView.RowHeadersDefaultCellStyle
3). DataGridView.ColumnHeadersDefaultCellStyle
4). DataGridView.DefaultCellStyle
单元格本身的设定的 Style 是最优先的.
privatevoid dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
- {
- int index = CardCMD.PublicInfoCom.GetLendRecordIndex();
- if (index ==0)
- {
- this.dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.Red;
- this.dataGridView1.Rows[0].Selected =false;
- this.dataGridView1.Rows[1].DefaultCellStyle.BackColor = Color.Wheat;
- this.dataGridView1.Rows[2].DefaultCellStyle.BackColor = Color.Wheat;
- }
- else if (index ==1)
- {
- this.dataGridView1.Rows[1].DefaultCellStyle.BackColor = Color.Red;
- this.dataGridView1.Rows[0].Selected =false;
- this.dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.Wheat;
- this.dataGridView1.Rows[2].DefaultCellStyle.BackColor = Color.Wheat;
- }
- else if (index ==2)
- {
- this.dataGridView1.Rows[2].DefaultCellStyle.BackColor = Color.Red;
- this.dataGridView1.Rows[0].Selected =false;
- this.dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.Wheat;
- this.dataGridView1.Rows[1].DefaultCellStyle.BackColor = Color.Wheat;
- }
- }
- privatevoid rowMergeView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
- {
- //updateview();
- for (int i =0; i <this.rowMergeView1.Rows.Count; i++)
- {
- try
- {
- this.rowMergeView1.Rows[i].DefaultCellStyle.BackColor = Color.FromName(softcolor[this.rowMergeView1.Rows[i].Cells["yujing"].Value.ToString()]);
- }
- catch (Exception ex)
- {
- new FileOper().writelog(ex.Message);
- }
- }
- }
- C# 中关于 DataGridView 行和列的背景色 - 前景色设置
来源: http://www.bubuko.com/infodetail-2562506.html