关键字
DefaultCellStyle
常用事件
RowPrePaint()
RowPrePaint()方法在发生任何单元格之前,行绘制时引发事件。
在自动添加新行时,背景颜色也会跟着绘制,在这个事件中。
主要关键语句
①
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.White;
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
②
var rowindex = dataGridView1.Rows[e.RowIndex];
rowindex.DefaultCellStyle.ForeColor = Color.White;
rowindex.DefaultCellStyle.BackColor = Color.Blue;
③
DataGridViewRow dgrRoe = dataGridView1.Rows[e.RowIndex];
dgrRoe.DefaultCellStyle.ForeColor = Color.White;//前景色
dgrRoe.DefaultCellStyle.BackColor = Color.Red;//背景色
②③都是先保存行,在设置背景颜色
Var
VAR可代替任何类型,编译器会根据上下文来判断你到底是想用什么类型,类似 OBJECT,但是效率比OBJECT高点。
前景色代表是字体
背景色代表是字体的背景
第一个设置颜色代码
if (e.RowIndex >= dataGridView1.Rows.Count - 1)
return;
else
{
Color Back = new Color();
Color Fore = new Color();
var rowindex = dataGridView1.Rows[e.RowIndex];
if (rowindex == dataGridView1.CurrentRow)
{
if (rowindex.DefaultCellStyle.ForeColor != Color.White)
{
Fore = rowindex.DefaultCellStyle.ForeColor;
rowindex.DefaultCellStyle.ForeColor = Color.White;
}
if (rowindex.DefaultCellStyle.BackColor != Color.Blue)
{
Back = rowindex.DefaultCellStyle.BackColor;
rowindex.DefaultCellStyle.BackColor = Color.Blue;
}
}
else
{
rowindex.DefaultCellStyle.ForeColor = Fore;
rowindex.DefaultCellStyle.BackColor = Back;
}
}
第二个设置颜色代码(与第一个设置方式类似,代码减少)
DataGridViewRow dgrRoe = dataGridView1.Rows[e.RowIndex];
if (e.RowIndex < dataGridView1.Rows.Count - 1)
{
if (dgrRoe == dataGridView1.CurrentRow)
{
dgrRoe.DefaultCellStyle.ForeColor = Color.White;//前景色
dgrRoe.DefaultCellStyle.BackColor = Color.Red;//背景色
}
else
{
dgrRoe.DefaultCellStyle.ForeColor = Color.SaddleBrown;
dgrRoe.DefaultCellStyle.BackColor = Color.Silver;
}
}
else { return; }
第三个设置颜色代码(索引与整行)
if (dataGridView1.Rows[e.RowIndex].Index >= dataGridView1.Rows.Count - 1)
{
return;
}
else
{
if (dataGridView1.Rows[e.RowIndex] == dataGridView1.CurrentRow)
{
//以下两种方法可行
i++;
//Console.WriteLine("执行次数:"+i+"\n行的索引:"+e.RowIndex);//每行执行三次,行索引为"当前鼠标点击那一行的索引"。
//dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.White;
//dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
Console.WriteLine("i的执行次数:" + i + "\n行的索引:" + dataGridView1.CurrentRow);//每行执行三次,行索引为"当前鼠标点击那一行的索引"。
dataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.White;
dataGridView1.CurrentRow.DefaultCellStyle.BackColor = Color.Red;
}
else
{
Console.WriteLine("j的执行次数:" + j + "\n行的索引:" + e.RowIndex);
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.SandyBrown;
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Orange;
}
}
第四个设置颜色代码(隔行)
第一种隔行的显示方式:
var datagrid = dataGridView1.Rows[0];
for (int i = 0; i < dataGridView1.Rows.Count; i += 2)
{
//隔一行颜色设置
if (i < dataGridView1.Rows.Count - 1)
{
datagrid = dataGridView1.Rows[i];
datagrid.DefaultCellStyle.ForeColor = Color.White;
datagrid.DefaultCellStyle.BackColor = Color.Blue;
}
}
第二种隔行的显示方式:
this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Pink;//行单元格默认样式
this.dataGridView1.AlternaingRowsDefaultCellStyle.BackColor = Color.Red;//奇数(单数)行的默认样式
第五个设置颜色代码(筛选)
筛选关键语句:
DataView dv = new DataView(dt,”gender = ‘女’”,”age Asc”,DataViewRowState.CurrentRows);
if (dataGridView1.CurrentRow.Index <= dataGridView1.Rows.Count - 1)
{
DataGridViewRow dgvr = dataGridView1.Rows[0];
for (int i = 0; i < dv.Count; i++)
{
dgvr = dataGridView1.Rows[i];
dgvr.DefaultCellStyle.ForeColor = Color.White;
dgvr.DefaultCellStyle.BackColor = Color.Blue;
}
}