Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to change the color of DataGridView rows according to conditions in C # development WinForm

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article editor for you a detailed introduction of the "C# development WinForm how to change the DataGridView row color according to the conditions", detailed content, clear steps, details handled properly, I hope this "C# development WinForm how to change the DataGridView row color according to conditions" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

You can use the RowPrePaint event to change the color of the DataGridView row based on the condition.

The interface of the sample program is as follows:

The sample program code is as follows:

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 ["Project Code"]. Value.ToString (). Trim (). Equals ("ACAC0001")) {/ / sets the background color of the cell dr.DefaultCellStyle.BackColor = Color.Yellow / / set the foreground color of the cell 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 'project code', XIANGMUMC AS 'project name', DANJIA AS 'unit price', SHULIANG AS 'quantity' 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 Read here, this article "how to change the color of DataGridView rows according to conditions in WinForm development of C #" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report