DataGridView添加行号隔行变色

运行效果

颜色对应关系

类实现代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    internal class DataGridViewLine: DataGridView
    {
        private void DefaultColorSet()
        {
            CellStyle_ForeColor = Color.Blue;
            CellStyle_BackColor = Color.Beige;
            CellStyle_SelectionForeColor = Color.Yellow;
            CellStyle_SelectionBackColor = Color.Orange;

            RowsDefaultCellStyle_BackColor = Color.Bisque;
            AlternatingRowsDefaultCellStyle_BackColor = Color.Beige;

        }
        public DataGridViewLine()
        {
            //默认颜色设置
            DefaultColorSet();

            this.CellPainting += new DataGridViewCellPaintingEventHandler(this.dataGridView1_CellPainting);
            SetFontAndColors();
        }
        //重绘界面
        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == -1)
            {
                e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground);
                using (Brush brush = new SolidBrush(e.CellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.CellStyle.Font, brush, e.CellBounds.Location.X + 10, e.CellBounds.Location.Y + 4);
                }
                e.Handled = true;
            }
            //SetFontAndColors();
        }

        //表格字体颜色设置
        private void SetFontAndColors()
        {
            this.DefaultCellStyle.Font = new Font(this.Font.Name, this.Font.Size);// new Font("Tahoma", 15);
            this.DefaultCellStyle.ForeColor = CellStyle_ForeColor;// Color.Blue;
            this.DefaultCellStyle.BackColor = CellStyle_BackColor;// Color.Beige;
            this.DefaultCellStyle.SelectionForeColor = CellStyle_SelectionForeColor;// Color.Yellow;
            this.DefaultCellStyle.SelectionBackColor = CellStyle_SelectionBackColor;// Color.Orange;

            this.RowsDefaultCellStyle.BackColor = RowsDefaultCellStyle_BackColor;// Color.Bisque;
            this.AlternatingRowsDefaultCellStyle.BackColor = AlternatingRowsDefaultCellStyle_BackColor;// Color.Beige;
        }

        private Color _CellStyle_ForeColor;
       [Category("Appearance")]
        [Description("单元格前景色。")]
        public Color CellStyle_ForeColor { 
            get { return _CellStyle_ForeColor; } 
            set { _CellStyle_ForeColor = value; 
                SetFontAndColors(); } 
        }
        private Color _CellStyle_BackColor;
        [Category("Appearance")]
        [Description("单元格背景色。")]
        public Color CellStyle_BackColor
        {
            get { return _CellStyle_BackColor; }
            set { _CellStyle_BackColor = value; SetFontAndColors(); }
        }
        private Color _CellStyle_SelectionForeColor;
        [Category("Appearance")]
        [Description("选中单元格前景色。")]
        public Color CellStyle_SelectionForeColor
        {
            get { return _CellStyle_SelectionForeColor; }
            set { _CellStyle_SelectionForeColor = value; SetFontAndColors(); }
        }
        private Color _CellStyle_SelectionBackColor;
        [Category("Appearance")]
        [Description("选中单元格背景色。")]
        public Color CellStyle_SelectionBackColor
        {
            get { return _CellStyle_SelectionBackColor; }
            set { _CellStyle_SelectionBackColor = value; SetFontAndColors(); }
        }
        private Color _RowsDefaultCellStyle_BackColor;
        [Category("Appearance")]
        [Description("偶数数行背景色。")]
        public Color RowsDefaultCellStyle_BackColor
        {
            get { return _RowsDefaultCellStyle_BackColor; }
            set { _RowsDefaultCellStyle_BackColor = value; SetFontAndColors(); }
        }
        private Color _AlternatingRowsDefaultCellStyle_BackColor;
        [Category("Appearance")]
        [Description("奇数数行背景色。")]
        public Color AlternatingRowsDefaultCellStyle_BackColor
        {
            get { return _AlternatingRowsDefaultCellStyle_BackColor; }
            set { _AlternatingRowsDefaultCellStyle_BackColor = value; SetFontAndColors(); }
        }


    }
}

测试代码:

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                int index = this.dataGridViewLine1.Rows.Add();//核心:通过返回值获取添加行的行索引
                this.dataGridViewLine1.Rows[index].Cells[0].Value = "1_"+ i.ToString();
                this.dataGridViewLine1.Rows[index].Cells[1].Value = "Esther";
                this.dataGridViewLine1.Rows[index].Cells[2].Value = "28";
                //如果有列标题,则可以用列标题的Name来赋值
                //this.dataGridView1.Rows[index].Cells["ID"].Value = "1";
                //this.dataGridView1.Rows[index].Cells["Name"].Value = "Esther";
                //this.dataGridView1.Rows[index].Cells["Age"].Value = "28";
            }
        }

参考链接

C#,WinForm DataGridView添加行号-罗分明网络博客 (luofenming.com)icon-default.png?t=N7T8https://www.luofenming.com/show.aspx?id=2024040316370374

特此记录

anlog

2024年4月17日

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/553012.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

二刷大数据(二)- Spark

目录 SparkHadoop区别核心组件运行架构Master&WorkerApplication (Driver)Executor RDD概念yarn下工作原理算子依赖血缘关系阶段划分广播变量 shuffle流程SparkSQLDataSet、DataFrame、RDD相互转换 SparkStreaming Spark Spark是一种基于内存的快速、通用、可扩展的大数据…

C# Solidworks二次开发:比较两个solidworks文档属性相关API详解

大家好&#xff0c;今天要讲的文章是关于如何比较两个solidworks文档。 下面是API的介绍&#xff1a; &#xff08;1&#xff09;第一个为Close&#xff0c;这个API的含义为在比较solidworks文档以后执行必要的清理。下面是官方的具体解释&#xff1a; 其没有输入参数&#x…

MySQL Workbench下载安装、 MySQL Workbench使用

官方下载链接;MySQL :: Download MySQL Workbench 下载好懒人安装&#xff0c;也可自己选择目录 下面是使用&#xff1a; 连接数据库&#xff1a; 填写数据库连接信息&#xff1a; 基本操作部分&#xff1a; 数据导入导出&#xff1a; 导出/备份 导入&#xff1a; 生产er图…

【机器学习】科学库使用第5篇:Matplotlib,学习目标【附代码文档】

机器学习&#xff08;科学计算库&#xff09;完整教程&#xff08;附代码资料&#xff09;主要内容讲述&#xff1a;机器学习&#xff08;常用科学计算库的使用&#xff09;基础定位、目标&#xff0c;机器学习概述定位,目标,学习目标,学习目标,1 人工智能应用场景,2 人工智能小…

react中关于类式组件和函数组件对props、state、ref的使用

文章中有很多蓝色字体为扩展链接&#xff0c;可以补充查看。 常用命令使用规则 组件编写方式: 1.函数式 function MyButton() { //直接return 标签体return (<>……</>); }2.类 class MyButton extends React.Component { //在render方法中&#xff0c;return…

UE5 C++ 射线检测

一.声明四个变量 FVector StartLocation;FVector ForwardVector;FVector EndLocation;FHitResult HitResult;二.起点从摄像机&#xff0c;重点为摄像机前9999m。射线检测 使用LineTraceSingleByChannel 射线直线通道检测&#xff0c;所以 void AMyCharacter::Tick(float Delt…

GPT国内能用吗

2022年11月&#xff0c;Open AI发布ChatGPT&#xff0c;ChatGPT展现了大型语模型在自然语言处理方面的惊人进步&#xff0c;其生成文本的流畅度和连贯性令人印象深刻&#xff0c;为AI应用打开了新的可能性。 ChatGPT的出现推动了AI技术在各个领域的应用&#xff0c;例如&#x…

Python学习教程(Python学习路线+Python学习视频):Python数据结构

数据结构引言&#xff1a; 数据结构是组织数据的方式&#xff0c;以便能够更好的存储和获取数据。数据结构定义数据之间的关系和对这些数据的操作方式。数据结构屏蔽了数据存储和操作的细节&#xff0c;让程序员能更好的处理业务逻辑&#xff0c;同时拥有快速的数据存储和获取方…

.net9 AOT编绎生成标准DLL,输出API函数教程-中国首创

1&#xff0c;安装VS2022预览版&#xff08;Visual Studio Preview&#xff09; https://visualstudio.microsoft.com/zh-hans/vs/preview/#download-preview 2&#xff0c;选择安装组件&#xff1a;使用C的桌面开发 和 .NET桌面开发 ------------------------------------- …

java八股文知识点讲解(个人认为讲的比较好的)

1、解决哈希冲突——链地址法&#xff1a;【第7章查找】19哈希表的查找_链地址法解决哈希冲突_哔哩哔哩_bilibili 2、解决哈希冲突——开放地址法 &#xff1a; 【第7章查找】18哈希表的查找_开放定址法解决哈希冲突_哔哩哔哩_bilibili 3、小根堆大根堆的创建&#xff1a;选择…

【每日刷题】Day17

【每日刷题】Day17 &#x1f955;个人主页&#xff1a;开敲&#x1f349; &#x1f525;所属专栏&#xff1a;每日刷题&#x1f34d; &#x1f33c;文章目录&#x1f33c; 1. 19. 删除链表的倒数第 N 个结点 - 力扣&#xff08;LeetCode&#xff09; 2. 162. 寻找峰值 - 力扣…

1 回归:锂电池温度预测top2 代码部分(一) Tabnet

2024 iFLYTEK A.I.开发者大赛-讯飞开放平台 TabNet&#xff1a; 模型也是我在这个比赛一个意外收获&#xff0c;这个模型在比赛之中可用。但是需要GPU资源&#xff0c;否则运行真的是太慢了。后面针对这个模型我会写出如何使用的方法策略。 比赛结束后有与其他两位选手聊天&am…

《ElementPlus 与 ElementUI 差异集合》el-popconfirm 气泡确认框之插槽写法有差异

ElementUI 直接在 el-button 上配置属性 slot&#xff1b; <el-popconfirm title"确定删除吗&#xff1f;请谨慎操作&#xff01;" confirm"delete"><el-button slot"reference" size"small" type"danger">删…

Word学习笔记之奇偶页的页眉与页码设置

1. 常用格式 在毕业论文中&#xff0c;往往有一下要求&#xff1a; 奇数页右下角显示、偶数页左下角显示奇数页眉为每章标题、偶数页眉为论文标题 2. 问题解决 2.1 前期准备 首先&#xff0c;不论时要求 1、还是要求 2&#xff0c;这里我们都要做一下设置&#xff1a; 鼠…

Adobe Firefly是否将重新定义AI视频编辑领域?|TodayAI

Adobe最近发布了一段令人瞩目的视频&#xff0c;详细展示了其最新推出的Adobe Firefly视频模型。这一模型集成了尖端的生成式人工智能技术&#xff0c;带来了一系列颠覆性的视频编辑功能&#xff0c;引发了业界的广泛关注和讨论。 视频中的旁白充满热情地宣布&#xff1a;“Ad…

【超级简单】vscode进入服务器的docker容器

前提 1、已经运行docker容器 2、已经用vscode链接服务器 在vscode中安装的插件 Dev Containers docker 在容器中安装的依赖 yum install openssh-server yum install openssh-clientsvscode进入服务器的docker容器 找到自己的容器&#xff0c;右键点击&#xff0c;找到…

Jmeter BeanShell调用Java方法加密

1、添加BeanShell前置处理器 由于请求接口时&#xff0c;会传加密参数。加密过程会在请求之前完成&#xff0c;所以需要使用前置处理器中beanshell preprocessor 2、编写BeanShell脚本 ①定义一个beashell变量&#xff1a;phoneNum&#xff0c;在Beanshell中可以直接调用Jmete…

idea运行报错:启动命令过长

JAVA项目&#xff0c;运行的时候报错 Command line is too long. Shorten the command line via JAR manifest or via a classpath file and rerun老问题了&#xff0c;记录一下 解决办法&#xff1a; 1、Edit Configurations 2、点击Modify options设置&#xff0c;勾选S…

janus架构学习

基础介绍 Janus 是由Meetecho设计和开发的开源、通用的基于SFU架构的WebRTC流媒体服务器&#xff0c;它支持在Linux的服务器或MacOS上的机器进行编译和安装。Janus 是使用C语言进行编写的&#xff0c;它的性能十分优秀。 架构 janus为sfu架构 模块结构图 模块说明 core模…

elementui 弹窗展示自动校验表单项bug

表单校验失败一次之后&#xff0c;再次弹出表单&#xff0c;触发自动校验 解决方案&#xff1a; clearValidate() 方法清空表单校验项 this.$nextTick(() > {this.$refs[checkForm].clearValidate() }) 使用nextTick规避报错
最新文章