C#编程-76:DataGridView当前行显示不同颜色_彭世瑜_新浪博客
发布日期:2021-07-01 06:17:57
浏览次数:2
分类:技术文章
本文共 1258 字,大约阅读时间需要 4 分钟。
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;
- }
- }
- }
- }
转载地址:https://pengshiyu.blog.csdn.net/article/details/109661798 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月16日 11时27分31秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Sublime Text 快捷键
2019-05-04
Vue 解决跨域问题
2019-05-04
读『开发App常见的九大错误』
2019-05-04
Freeline - Android平台上的秒级编译方案
2019-05-04
美股课堂:美国银行开户亲历记
2019-05-05
逆生长_百度百科
2019-05-05
“秀才造反,十年不成”是什么意思? - 已解决 - 搜搜问问
2019-05-05
服装配饰_MAVIN MARVY 高级服装定制_西服定制_衬衫定制_西装定制
2019-05-05
第九课堂-经验与技能分享交易网站
2019-05-05
北京西服定做_衬衫定制_关于我们_Dimoon TLR.
2019-05-05
何谓Dandy?它是一种着装风格
2019-05-05
景甜_百度百科
2019-05-05
Squid配置文件详解
2019-05-05
(12) Hadoop 升级技能
2019-05-06
(总结)Nginx配置文件nginx.conf中文详解
2019-05-06
wget 下载
2019-05-06
HTTPS的七个误解
2019-05-06
ssl证书配置
2019-05-06
为什么使用<!DOCTYPE HTML>
2019-05-06