我的datagridview是隔行颜色变化的那种,就是奇数行是白色的,偶数行是灰色的。
datagridview里面有个defaultcellsytle属性,在那儿可以设置selectionbackcolor,默认的是蓝色的,
所以当我选中我的datagridview的某一行的时候,就变成蓝色的了。我现在的要求是,当选中某一行的时候,颜色没有变化,就是说奇数行还是白色,偶数行还是灰色,保持它们本来的颜色。应该怎么做?
datagridview里面有个defaultcellsytle属性,在那儿可以设置selectionbackcolor,默认的是蓝色的,
所以当我选中我的datagridview的某一行的时候,就变成蓝色的了。我现在的要求是,当选中某一行的时候,颜色没有变化,就是说奇数行还是白色,偶数行还是灰色,保持它们本来的颜色。应该怎么做?
解决方案 »
- System.timers.timer数组问题,
- VS 数据绑定问题
- C#的windows窗体中如何实现窗体的动态变化?
- 一个asp.net 2.0的master/content Page 问题
- 提交页面时为什么不触发datagrid的 databound事件啊
- MVP,高手们帮帮忙,关于如何使属性窗口控件(PropertyGrid),SelectedObject父类的属性呀!急!
- 我用C#开发ASP。NET程序出现问题
- SQL Server 中char类型如何实现精确查询?
- C#中想在ACCESS中插入记录总是不成功?
- WPF,该如何理解ActualWidth属性
- c#怎么实现显示波形?
- C#winform中放有多个picturebox控件,如何让称动一个时,其它的不动?
DataGridViewCellStyle { SelectionBackColor=Color [White], SelectionForeColor=Color [Black] }
DataGridView1.DefaultCellStyle.SelectionBackColor = Color.White
DataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black
DataGridView1.AlternatingRowsDefaultCellStyle.SelectionBackColor = Color.Gray
DataGridView1.AlternatingRowsDefaultCellStyle.SelectionForeColor = Color.Black
DataGridView1.DefaultCellStyle.SelectionBackColor = Color.White
DataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black
DataGridView1.AlternatingRowsDefaultCellStyle.SelectionBackColor = Color.Gray
DataGridView1.AlternatingRowsDefaultCellStyle.SelectionForeColor = Color.Black
AlternatingRowsDefaultCellStyle只是隔行的。
我親自測試過的,和你要求的效果一樣的。
{
#region 单元格交替颜色
this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
#endregion
} private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.CurrentCell.RowIndex % 2 == 1)
{
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Beige;
}
else
{
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Bisque;
}
}控件隔行显示不同的颜色,
选中后的颜色不改变,保持原来的颜色。
private void Form1_Load(object sender, EventArgs e)
{
//设置奇偶行颜色
dataGridView1.RowsDefaultCellStyle.BackColor = Color.White;
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige; //设置奇偶行选中的颜色
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White ;
dataGridView1.AlternatingRowsDefaultCellStyle.SelectionBackColor = Color.Beige;
}
我的datagrid还有一个特点
根据datatable的某一个字段,如果为1,颜色变成红色
选中该行的时候这个红色也要求维持原样
{
//这里面自己判断呗
好象取得的总是defaultcellsytle的selectionbackcolor
this.BackColor = dataGridView1.CurrentCell.Style.BackColor;
好像可以,不过返回默认总的Empty,并没有返回设置的值。
就是计算出当前行在屏幕上的坐标,用api取得屏幕点的颜色!