关于DataGridView行和列的背景色-前景色设置

1.设定DataGridView全部单元格的Style
  DataGridView内所有单元格的Style变更,可以使用DataGridView对象的DefaultCellStyle属性实现。

1
2
3
4
5
//包含Header所有的单元格的背景色为黄色
DataGridView1.DefaultCellStyle.BackColor = Color.Yellow;
 
//包含Header所有的单元格的前景色为黄色
DataGridView1.DefaultCellStyle.ForeColor= Color.Yellow; //前景色设置,只需要将BackColor改为ForeColor即可

2.DataGridView.DefaultCellStyle属性可以对包含Header所有单元格的Style进行变更设定,对除 Header以外所  有单元格的Style进行变更,可以使用DataGridView.RowsDefaultCellStyle属性实现

1
2
// Header以外所有的单元格的背景色为黄色
DataGridView1.RowsDefaultCellStyle.BackColor = Color.Yellow;

3.变更某一个单元格的Style
  DataGridViewCell.Style属性可以对单一的单元格的Style进行变更设定。
  如下面的例子,只对(0, 0)单元格的背景色设定为粉红色。

1
2
//(0, 0)单元格的背景色为粉色
DataGridView1[0, 0].Style.BackColor = Color.Pink;

4.变更被指定的列、行的单元格的Style
  DataGridViewColumn.DefaultCellStyle属性,可以对列的单元格Style进行变更设定。  DataGridViewRow.DefaultCellStyle属性,可以对行的单元格Style进行变更设定。
  如下面的例子,第一列的单元格的背景色为淡蓝色,第一行的单元格的背景色为淡灰色。

1
2
3
4
5
//索引0列的单元格的背景色为淡蓝色
DataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.Aqua;
 
//索引0行的单元格的背景色为淡灰色
DataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.LightGray;

5.变更奇数行的单元格Style
  DataGridView.AlternatingRowsDefaultCellStyle属性,可以变更DataGridView的奇数行的单元格 Style。
  如下面的例子,奇数行的单元格的背景色设定为黄绿色

1
2
//奇数行的单元格的背景色为黄绿色
DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.GreenYellow;

6.变更列Header、行Header的单元格Style
  列Header的单元格style的变更,可以使用,DataGridView.ColumnHeadersDefaultCellStyle属性实现。行  Header的单元格Style的变更,可以使用DataGridView.RowHeadersDefaultCellStyle属性实现。但是,Header  的是左侧的单元格需要通过DataGridView.TopLeftHeaderCell属性,取得的DataGridViewHeaderCell对象的单    元格Style进行设定。
  如下面的例子,列Header的背景色为象牙色,行Header的背景色为橙色。

1
2
3
4
5
//列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 ==)
  {
    this.dataGridView1.Rows[].DefaultCellStyle.BackColor = Color.Red;
    this.dataGridView1.Rows[].Selected =false;
    this.dataGridView1.Rows[].DefaultCellStyle.BackColor = Color.Wheat;
    this.dataGridView1.Rows[].DefaultCellStyle.BackColor = Color.Wheat;
  }
  else if (index ==)
  {
    this.dataGridView1.Rows[].DefaultCellStyle.BackColor = Color.Red;
    this.dataGridView1.Rows[].Selected =false;
    this.dataGridView1.Rows[].DefaultCellStyle.BackColor = Color.Wheat;
    this.dataGridView1.Rows[].DefaultCellStyle.BackColor = Color.Wheat;
  }
  else if (index ==)
  {
    this.dataGridView1.Rows[].DefaultCellStyle.BackColor = Color.Red;
    this.dataGridView1.Rows[].Selected =false;
    this.dataGridView1.Rows[].DefaultCellStyle.BackColor = Color.Wheat;
    this.dataGridView1.Rows[].DefaultCellStyle.BackColor = Color.Wheat;
  }
}
privatevoid rowMergeView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
  //updateview();
  for (int i =; 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行和列的背景色-前景色设置的更多相关文章

  1. [转]python中pandas库中DataFrame对行和列的操作使用方法

    转自:http://blog.csdn.net/u011089523/article/details/60341016 用pandas中的DataFrame时选取行或列: import numpy a ...

  2. python中pandas库中DataFrame对行和列的操作使用方法

    用pandas中的DataFrame时选取行或列: import numpy as np import pandas as pd from pandas import Sereis, DataFram ...

  3. winform中的DataGridView的列宽设置

    DataGridView有一个属性AutoSizeColumnMode,他有很多枚举值: 1.AllCells 调整列宽,以适合该列中的所有单元格的内容,包括标题单元格.  2.AllCellsExc ...

  4. jQuery实现表格冻结行和列

    前几天,遇到一个需求是要将表格的前几行和前几列冻结即固定,就是在有滚动条的情况下,保持那几行和那几列固定,这个需求其实是一个非常常见的需求,因为在涉及好多行和列时,在拖动滚动条时,我们需要知道每行每列 ...

  5. php实例源码之获取mysql表中所有行和列

    本文章向大家介绍php获取mysql表中所有行和列的源码,主要使用到mysql_num_rows和mysql_fetch_row等php的数据库操作函数,该实例有助于大家熟悉PHP mysql数据库编 ...

  6. PyQt(Python+Qt)学习随笔:QStandardItemModel指定行和列创建模型中的项以及索引

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.引言 QStandardItemModel有两种构造方法: QStandardItemModel ...

  7. 自定义datagridview列,却提示DataGridView 控件中至少有一列没有单元格模板

    哈哈,一个小误区,你看看设计窗体生成的代码,DataGridView的列不是GridViewColumn 而是DataGridViewTextBoxColumn你只要添加这个类型的对象就可以了,我也是 ...

  8. .NET组件控件实例编程系列——5.DataGridView数值列和日期列

    在使用DataGridView编辑数据的时候,编辑的单元格一般会显示为文本框,逻辑值和图片会自动显示对应类型的列.当然我们自己可以手工选择列的类型,例如ComboBox列.Button列.Link列. ...

  9. 【jQuery 冻结任意行列】冻结任意行和列的jQuery插件

    实现原理: 创建多个div,div之间通过css实现层叠,每个div放置当前表格的克隆.例如:需要行冻结时,创建存放冻结行表格的div,通过设置z-index属性和position属性,让冻结行表格在 ...

随机推荐

  1. 方法(method)和函数(function)有什么区别?

    方法(method)和函数(function)有什么区别? 定义和参数区别 函数是独立的功能,与对象无关,需要显示的传递数据 方法与对象和类相关,依赖对象而调用,可以直接处理对象上的数据,也就是隐式传 ...

  2. Python调用Windows外部程序

    在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其他脚本,或者程序提供的功能,而不必再次编写实现该功能的代码.为了更好地控制运行的进程,可以使用win32pro ...

  3. HTTPS与HTTP协议区别

    1.主要区别 (1)超文本传输协议HTTP,HTTP下加入SSL层(位于传输层)的安全协议. (2)HTTP端口为80,HTTPS端口是443. 2.HTTPS通信过程 https 通信传输过程:

  4. RN组件备忘录

    1:ActivityIndicator:圆形的loading提示符号. 2:Button:按钮 3:FlatList:高性能列表组件,支持下拉刷新. 4:Image:图片组件,能显示 网络图片.静态资 ...

  5. SSM实战——秒杀系统之高并发优化

    一:高并发点 高并发出现在秒杀详情页,主要可能出现高并发问题的地方有:秒杀地址暴露.执行秒杀操作. 二:静态资源访问(页面)优化——CDN CDN,内容分发网络.我们把静态的资源(html/css/j ...

  6. 在openerp撰写消息中增加图片

    openerp的撰写消息中, 在文本输入框中, 具有设置文本字体,设置对齐方式 等多种功能, 就像像写这篇新浪blog一样, 可以输入富文本信息. 美中不足的是, 它不能插入图片. 我们如何才能让op ...

  7. java 线程池线程忙碌且阻塞队列也满了时给一个拒接的详细报告

    线程池线程忙碌且阻塞队列也满了时给一个拒接的详细报告.下面是一个自定义的终止策略类,继承了ThreadPoolExecutor.AbortPolicy类并覆盖了rejectedExecution方法把 ...

  8. 转 linux Crontab 使用

    cron用法说明 cron的用法老是记不住,索性写下来备忘.下文内容大部分是根据<Cron Help Guide>翻译而来,有些部分是自己加上的. 全文如下: cron来源于希腊单词chr ...

  9. Red Hat7.2 上安装 MySQL5.5.58

    1.首先查看linux版本:cat /etc/redhat-release Red Hat Enterprise Linux Server release 7.2 (Maipo) 2.Linux查看版 ...

  10. Ubuntu18.04下的音频录制和编辑软件Ardour及QjackCtl(jackd gui)

    Ardour 是一个Linux和OSX下的多音轨录制和数字音频编辑软件. 需要配合ALSA或者JACK总线使用. 快速入门 http://brunoruviaro.github.io/ardour4- ...