var 值类型
object 引用类型
\
实现效果:
\
实现代码:
\
-
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Linq;
-
using System.Text;
-
using System.Windows.Forms;
-
-
namespace DataGridViewColor
-
{
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
-
private void Form1_Load(object sender, EventArgs e)
-
{
-
// TODO: 这行代码将数据加载到表“companyDataSet.clerk”中。您可以根据需要移动或删除它。
-
this.clerkTableAdapter.Fill(this.companyDataSet.clerk);
-
-
}
-
-
private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
-
{
-
//判断索引有效范围
-
if(e.RowIndex>=dataGridView1.Rows.Count-1) return;
-
-
//存储原来的颜色值
-
Color oldForeColor = new Color();
-
Color oldBackColor = new Color();
-
-
//判断传入的行是否为当前选中行
-
var row=dataGridView1.Rows[e.RowIndex];
-
if (row == dataGridView1.CurrentRow)
-
{
-
//设置前景色
-
if (row.DefaultCellStyle.ForeColor != Color.White)
-
{
-
oldForeColor = row.DefaultCellStyle.ForeColor;
-
row.DefaultCellStyle.ForeColor = Color.White;
-
}
-
//设置背景色
-
if (row.DefaultCellStyle.BackColor != Color.Blue)
-
{
-
oldBackColor = row.DefaultCellStyle.BackColor;
-
row.DefaultCellStyle.BackColor = Color.Blue;
-
}
-
-
}
-
//未选中则恢复原来的颜色
-
else
-
{
-
row.DefaultCellStyle.ForeColor = oldForeColor;
-
row.DefaultCellStyle.BackColor = oldBackColor;
-
}
-
}
-
}
-
}