最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#開發(fā)WinForm根據(jù)條件改變DataGridView行顏色

 更新時間:2022年03月07日 16:46:03   作者:.NET開發(fā)菜鳥  
這篇文章介紹了C#開發(fā)WinForm根據(jù)條件改變DataGridView行顏色的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

根據(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["項目代碼"].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 '項目代碼',XIANGMUMC AS '項目名稱', DANJIA AS '單價',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;
        }
    }
}

示例程序下載地址:點此下載

到此這篇關(guān)于C#開發(fā)WinForm根據(jù)條件改變DataGridView行顏色的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

宁夏| 凤台县| 平泉县| 叙永县| 宜良县| 游戏| 洪泽县| 樟树市| 泌阳县| 弥渡县| 堆龙德庆县| 太仆寺旗| 永新县| 庆城县| 洛扎县| 望谟县| 锦屏县| 临沂市| 大港区| 文山县| 花垣县| 长汀县| 大同县| 山阳县| 社旗县| 新密市| 临猗县| 定日县| 萝北县| 富川| 淮北市| 呼伦贝尔市| 伊金霍洛旗| 托克逊县| 喜德县| 将乐县| 海淀区| 凤阳县| 西乌珠穆沁旗| 图片| 丹寨县|