C#開(kāi)發(fā)WinForm根據(jù)條件改變DataGridView行顏色
根據(jù)條件改變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["項(xiàng)目代碼"].Value.ToString().Trim().Equals("ACAC0001")) { // 設(shè)置單元格的背景色 dr.DefaultCellStyle.BackColor = Color.Yellow; // 設(shè)置單元格的前景色 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 '項(xiàng)目代碼',XIANGMUMC AS '項(xiàng)目名稱(chēng)', DANJIA AS '單價(jià)',SHULIANG AS '數(shù)量' 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; } } }
示例程序下載地址:點(diǎn)此下載
到此這篇關(guān)于C#開(kāi)發(fā)WinForm根據(jù)條件改變DataGridView行顏色的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解C#中 Thread,Task,Async/Await,IAsyncResult的那些事兒
本文主要介紹了C#中 Thread,Task,Async/Await,IAsyncResult的相關(guān)知識(shí)。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01C#獲取客戶(hù)端相關(guān)信息實(shí)例總結(jié)
這篇文章主要介紹了C#獲取客戶(hù)端相關(guān)信息的方法,以實(shí)例形式總結(jié)了C#獲取客戶(hù)端IP地址、網(wǎng)絡(luò)連接、硬件信息等相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09C# 使用Fiddler捕獲本地HttpClient發(fā)出的請(qǐng)求操作
這篇文章主要介紹了C# 使用Fiddler捕獲本地HttpClient發(fā)出的請(qǐng)求操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10C# Winform 讓整個(gè)窗口都可以拖動(dòng)
Windows 的 API 果然強(qiáng)大啊.以前要實(shí)現(xiàn)全窗口拖動(dòng), 要寫(xiě)鼠標(biāo)按下和抬起事件, 很是麻煩, 偶爾還會(huì)出現(xiàn) BUG2011-05-05c#實(shí)現(xiàn)最簡(jiǎn)潔的快速排序(你絕對(duì)可以看懂)
這篇文章主要給大家介紹了關(guān)于利用c#實(shí)現(xiàn)如何最簡(jiǎn)潔的快速排序,實(shí)現(xiàn)的方法你絕對(duì)可以看懂,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05C#中GraphicsPath的Flatten方法用法實(shí)例
這篇文章主要介紹了C#中GraphicsPath的Flatten方法,實(shí)例分析了Flatten方法的相關(guān)使用技巧,需要的朋友可以參考下2015-06-06