C#开发WinForm根据条件改变DataGridView行颜色
作者:.NET开发菜鸟 时间:2022-05-06 05:38:56
根据条件改变DataGridView行的颜色可以使用RowPrePaint事件。
示例程序界面如下:
示例程序代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
namespace DgvChangeColor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string strCon = ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString;
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = GetDataSource();
this.DgvColor.DataSource = dt;
}
private void DgvColor_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
if (e.RowIndex >= DgvColor.Rows.Count - 1)
{
return;
}
DataGridViewRow dr = (sender as DataGridView).Rows[e.RowIndex];
if (dr.Cells["项目代码"].Value.ToString().Trim().Equals("ACAC0001"))
{
// 设置单元格的背景色
dr.DefaultCellStyle.BackColor = Color.Yellow;
// 设置单元格的前景色
dr.DefaultCellStyle.ForeColor = Color.Black;
}
else
{
dr.DefaultCellStyle.BackColor = Color.Blue;
dr.DefaultCellStyle.ForeColor = Color.White;
}
}
private DataTable GetDataSource()
{
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(strCon);
string strSQL = "SELECT XIANGMUCDDM AS '项目代码',XIANGMUMC AS '项目名称', DANJIA AS '单价',SHULIANG AS '数量' FROM InPatientBillDt WHERE 就诊ID='225600'";
SqlCommand cmd = new SqlCommand(strSQL, conn);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
try
{
conn.Open();
adapter.Fill(dt);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return dt;
}
}
}
示例程序下载地址:点此下载
来源:https://www.cnblogs.com/dotnet261010/p/8464343.html
标签:C#,WinForm,DataGridView,改变,行,颜色
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
RocketMq深入分析讲解两种削峰方式
2023-04-04 01:38:47
解决response.setHeader设置下载文件名无效的问题
2021-08-15 20:43:54
SpringBoot定时任务多线程实现示例
2021-10-06 08:45:18
![](https://img.aspxhome.com/file/2023/1/71021_0s.png)
基于Java随机生成手机短信验证码的实例代码
2023-12-22 02:50:49
Java实现求数组最长子序列算法示例
2023-09-28 12:35:14
Java并发问题之乐观锁与悲观锁
2022-07-13 19:59:05
![](https://img.aspxhome.com/file/2023/1/84201_0s.png)
Java文件操作之IO流 File类的使用详解
2023-07-26 00:49:41
在Eclipse安装Spring boot插件的步骤(图文)
2023-01-14 05:04:05
![](https://img.aspxhome.com/file/2023/2/78352_0s.png)
C语言文件操作之fread函数详解
2023-07-06 18:24:15
![](https://img.aspxhome.com/file/2023/7/103077_0s.jpg)
Logger.error打印错误异常的详细堆栈信息
2022-01-06 23:03:04
![](https://img.aspxhome.com/file/2023/3/73033_0s.jpg)
Spring集成Swagger常见错误及解决办法
2023-07-10 05:01:17
![](https://img.aspxhome.com/file/2023/9/88749_0s.png)
如何使用C#从word文档中提取图片
2022-04-17 06:58:09
![](https://img.aspxhome.com/file/2023/7/89747_0s.png)
android 通过MediaRecorder实现简单的录音示例
2023-07-29 06:03:54
C# 开发step步骤条控件详解
2021-07-01 00:53:14
![](https://img.aspxhome.com/file/2023/0/80040_0s.png)
使用EasyPoi轻松导入导出Excel文档的方法示例
2023-12-26 11:47:56
![](https://img.aspxhome.com/file/2023/8/92638_0s.png)
SpringBoot整合Pulsar的实现示例
2021-10-09 17:39:35
java 可变参数详解及实例
2021-06-01 10:02:42
mybatisPlus实现倒序拼接字符串
2021-11-16 08:50:29
Java设计模式之模版方法模式简介
2023-07-16 14:58:32
![](https://img.aspxhome.com/file/2023/0/61130_0s.jpg)
Java基于ShardingSphere实现分库分表的实例详解
2022-04-20 18:43:37
![](https://img.aspxhome.com/file/2023/0/76270_0s.jpg)