zoukankan      html  css  js  c++  java
  • DataGridView:根据条件改变单元格的颜色

    根据条件改变DataGridView行的颜色可以使用RowPrePaint事件。

    示例程序界面如下:

    示例程序代码如下:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 using System.Windows.Forms;
    10 using System.Configuration;
    11 using System.Data.SqlClient;
    12 
    13 namespace DgvChangeColor
    14 {
    15     public partial class Form1 : Form
    16     {
    17         public Form1()
    18         {
    19             InitializeComponent();
    20         }
    21 
    22         string strCon = ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString;
    23         private void Form1_Load(object sender, EventArgs e)
    24         {
    25             DataTable dt = GetDataSource();
    26             this.DgvColor.DataSource = dt;
    27         }
    28 
    29         private void DgvColor_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
    30         {
    31             if (e.RowIndex >= DgvColor.Rows.Count - 1)
    32             {
    33                 return;
    34             }
    35             DataGridViewRow dr = (sender as DataGridView).Rows[e.RowIndex];
    36 
    37             if (dr.Cells["项目代码"].Value.ToString().Trim().Equals("ACAC0001"))
    38             {
    39                 // 设置单元格的背景色
    40                 dr.DefaultCellStyle.BackColor = Color.Yellow;
    41                 // 设置单元格的前景色
    42                 dr.DefaultCellStyle.ForeColor = Color.Black;
    43             }
    44             else
    45             {
    46                 dr.DefaultCellStyle.BackColor = Color.Blue;
    47                 dr.DefaultCellStyle.ForeColor = Color.White;
    48             }
    49         }
    50 
    51         private DataTable GetDataSource()
    52         {
    53             DataTable dt = new DataTable();
    54             SqlConnection conn = new SqlConnection(strCon);
    55             string strSQL = "SELECT XIANGMUCDDM AS '项目代码',XIANGMUMC AS '项目名称', DANJIA AS '单价',SHULIANG AS '数量' FROM InPatientBillDt WHERE 就诊ID='225600'";
    56             SqlCommand cmd = new SqlCommand(strSQL, conn);
    57             SqlDataAdapter adapter = new SqlDataAdapter();
    58             adapter.SelectCommand = cmd;
    59             try
    60             {
    61                 conn.Open();
    62                 adapter.Fill(dt);
    63             }
    64             catch (Exception ex)
    65             {
    66                 MessageBox.Show(ex.Message);
    67             }
    68             finally
    69             {
    70                 conn.Close();
    71             }
    72             return dt;
    73         }
    74     }
    75 }

     示例程序下载地址:https://pan.baidu.com/s/1sm2eSlZ

  • 相关阅读:
    我们应当如何做需求分析
    C++ 转型动作 尽量避免 以及 那些意想不到的威胁
    Jetty学习二:配置概览-怎么配置Jetty
    Android手机APN设置(中国移动 联通3G 电信天翼),解决不能上网的问题
    CreateFile使用方法和样例
    Installation of NVIDIA Drivers in RHEL/CentOS and Fedora
    【视频分享】Liger UI实战集智建筑project管理系统配商业代码(打印报表、角色式权限管理)
    JAVA实现HTTPserver端
    LeetCode: Implement strStr() [027]
    c++反汇编与逆向分析 小结
  • 原文地址:https://www.cnblogs.com/dotnet261010/p/8464343.html
Copyright © 2011-2022 走看看