DataGridView 隔行显示不同的颜色
两种方法
第一种
DataGridview1.Rows[i].DefultCellStyle.backcolor
第二种
AlternatingRowsDefutCellstyle 属性
获取或设置应用于DataGridview的奇数行的默认单元格样式。 RowsDefultCellStyle 属性
获取或设置应用于DataGridview的行单元格的默认样式。 private void Form1_Load(object sender, EventArgs e)
{
string str = "server=192.168.100.222;user=sa;pwd=p@ssw1rd;database=pwd1";
SqlConnection mycon = new SqlConnection(str);
try
{
mycon.Open();
DataSet mydt = new System.Data.DataSet();//建立填充数据库
SqlDataAdapter mydpt = new SqlDataAdapter("select * from book",mycon);//建立适配器
mydpt.Fill(mydt);
dataGridView1.DataSource = mydt.Tables[0];//数据源绑定的是表不是数据库,所以要指定表,索引值从0开始 说明book这个表是数据库中第一个表 for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (i % 2 == 0)
{
this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Beige;
}
else
{
this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red; }
} }
catch (Exception ex)
{ MessageBox.Show(ex.Message);
}
finally
{
mycon.Close(); }
}
private void Form1_Load(object sender, EventArgs e)
{
string str = "server=192.168.100.222;user=sa;pwd=p@ssw1rd;database=pwd1";
SqlConnection mycon = new SqlConnection(str);
try
{
mycon.Open();
DataSet mydt = new System.Data.DataSet();//建立填充数据库
SqlDataAdapter mydpt = new SqlDataAdapter("select * from book",mycon);//建立适配器
mydpt.Fill(mydt);
dataGridView1.DataSource = mydt.Tables[];//数据源绑定的是表不是数据库,所以要指定表,索引值从0开始 说明book这个表是数据库中第一个表 for (int i = ; i < dataGridView1.Rows.Count; i++)
{
if (i % == )
{
this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Beige;
}
else
{
this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red; }
} //第二种方法
this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Red;//DataGridView行单元格默认颜色
this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Violet;//奇数行单元格默认颜色 }
catch (Exception ex)
{ MessageBox.Show(ex.Message);
}
finally
{
mycon.Close(); }
}
DataGridView 隔行显示不同的颜色的更多相关文章
- DataGridView隔行显示不同的颜色
如果该dataGridView是跟数据库绑定的,则可以触发DataBindingComplete事件: 1private void dataGridView1_DataBindingCo ...
- winform中dataGridView隔行显示不同的背景色,鼠标移动上显示不同颜色,离开后变回原色
winform中dataGridView隔行显示不同的背景色,鼠标移动上显示不同颜色,离开后变回原色 先设置奇数行颜色,这个有个自带的属性AlternatingRowsDefaultCellStyle ...
- (转)用JS实现表格中隔行显示不同颜色
用JS实现表格中隔行显示不同颜色 第一种: <style> tr{bgColor:expression( this.bgColor=((this.rowIndex)%2==0 )? ...
- Button四角有弧度及按下显示不同的颜色
一般的button都是矩形或者正方形,但为了显示不同的效果,让界面更美化,可以对其进行处理!!! 1.四角有弧度的button 2.按下button显示不同的颜色 实现步骤: 首先在drawable文 ...
- DataGridView 分页显示
DataGridView 分页显示函数 1.获取当前页的子数据表函数 public static DataTable GetPagedTable(DataTable dt, int PageIndex ...
- Lable 控件 -- 用代码改变要显示字体的颜色
lable控件怎么改变显示字体的颜色 代码如下: string color = "#B72C34"; this.lbl.ForeColor = System.Drawing.Col ...
- C#将dataGridView中显示的数据导出到Excel(大数据量超有用版)
开发中非常多情况下须要将dataGridView控件中显示的数据结果以Excel或者Word的形式导出来,本例就来实现这个功能. 因为从数据库中查找出某些数据列可能不是必需显示出来,在dataGrid ...
- winform datagridview 不显示默认第一列 不显示未绑定列 数据源发生改变时自动更新 (转)
不显示带星号的第一列: datagridview属性框中将 RowHeadersVisiber 设置为 false 不显示未绑定列: datagridview有一个属性是 AutoGenerateC ...
- reportservice报表单元格依据条件显示不同的颜色
有时候.我们须要依据条件,让单元格显示不同的颜色: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveV9mMTIz/font/5a6L5L2T/fontsi ...
随机推荐
- This InfoPath form template is browser-compatible, but it cannot be browser-enabled on the selected site
- all features were running on sitecollection level and at site level But here is the solution, i do ...
- deamo的网址
https://blog.csdn.net/jydave/article/details/50656455 广播(Broadcast)的简单用法 http://www.cgtsj.com/cg/ ...
- Bootstrap Table使用方法详解
http://www.jb51.net/article/89573.htm bootstrap-table使用总结 bootstrap-table是在bootstrap-table的基础上写出来的,专 ...
- 题目1002:Grading(简单判断)
问题来源 http://ac.jobdu.com/problem.php?pid=1002 问题描述 题目背景为高考试卷批改打分制度.对于每一道题,至少需要两位评审老师进行打分, 当两个老师的打分结果 ...
- python3.6的request
request实例1: import requests payload = {'key1':'value','key2':'value2'} url = "http://httpbin.or ...
- C#数组,List,Dictionary,IQueryable,IEnumerable的相互转换
本篇文章会向大家实例讲述以下内容: 将数组转换为List 将List转换为数组 将数组转换为Dictionary 将Dictionary 转换为数组 将List转换为Dictionary 将Dicti ...
- docker入门之基础操作
docker,我们可以把docker当作是简单的虚拟机.但这个虚拟机不像vm一样安装系统.所以我们又称之为容器.你可以理解成,容器就是虚拟机 docker与vm的对比 vmware:下载镜像——安装系 ...
- LinkedList简要分析
LinkedList概述 LinkedList 实现List接口,底层是双向链表,非线程安全.LinkedList还可以被当作堆栈.队列或双端队列进行操作.在JDK1.7/8 之后取消了循环,修改为双 ...
- Android Studio 学习笔记1.1 创建自己的第一个安卓项目并且打包APK
自从上一次安装完安卓开发工具Android Studio后抽时间看视屏尝试编写自己的第一个安卓项目约两周的时间 每天下班后会花上1~2小时的时间去学习 目前的成果如下:次元宅的我.apk 嘛 总而 ...
- pymongo认证连接
有的MongoDB数据库使用了认证功能,需要认证连接才能正常登录. mongoDB有不同的认证机制,3.0版本以后采用的是'SCRAM-SHA-1', 之前的版本采用的是'MONGODB-CR'.所以 ...