DataGridView单元格代码控制

本文介绍了一个使用C#实现的DataGridView控件案例,该控件能够根据数据的不同展示定制化的视觉效果,例如根据“Balance”列的数值改变背景颜色,以及用不同图标替换“Priority”列的文字描述。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;

using System.Drawing;

using System.Windows.Forms;



namespace DataGridViewTest

{

    public partial class Form1 : Form

    {

        private DataGridView dataGridView1 = new DataGridView();

        private Bitmap highPriImage;

        private Bitmap mediumPriImage;

        private Bitmap lowPriImage;



        public Form1()

        {

            InitializeComponent();



            // Initialize the images. 

            try

            {

                highPriImage = new Bitmap("highPri.bmp");

                mediumPriImage = new Bitmap("mediumPri.bmp");

                lowPriImage = new Bitmap("lowPri.bmp");

            }

            catch (ArgumentException)

            {

                MessageBox.Show("The Priority column requires Bitmap images " +

                    "named highPri.bmp, mediumPri.bmp, and lowPri.bmp " +

                    "residing in the same directory as the executable file.");

            }



            // Initialize the DataGridView.

            dataGridView1.Dock = DockStyle.Fill;

            dataGridView1.AllowUserToAddRows = false;

            dataGridView1.Columns.AddRange(

                new DataGridViewTextBoxColumn(),

                new DataGridViewImageColumn());

            dataGridView1.Columns[0].Name = "Balance";

            dataGridView1.Columns[1].Name = "Priority";

            dataGridView1.Rows.Add("-100", "high");

            dataGridView1.Rows.Add("0", "medium");

            dataGridView1.Rows.Add("100", "low");

            dataGridView1.CellFormatting +=

                new System.Windows.Forms.DataGridViewCellFormattingEventHandler(

                this.dataGridView1_CellFormatting);

            this.Controls.Add(dataGridView1);



        }



        // Changes how cells are displayed depending on their columns and values.

        private void dataGridView1_CellFormatting(object sender,

            System.Windows.Forms.DataGridViewCellFormattingEventArgs e)

        {

            // Set the background to red for negative values in the Balance column.

            if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Balance"))

            {

                Int32 intValue;

                if (Int32.TryParse((String)e.Value, out intValue) &&

                    (intValue < 0))

                {

                    e.CellStyle.BackColor = Color.Red;

                    e.CellStyle.SelectionBackColor = Color.DarkRed;

                }

            }



            // Replace string values in the Priority column with images.

            if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Priority"))

            {

                // Ensure that the value is a string.

                String stringValue = e.Value as string;

                if (stringValue == null) return;



                // Set the cell ToolTip to the text value.

                DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];

                cell.ToolTipText = stringValue;



                // Replace the string value with the image value.

                switch (stringValue)

                {

                    case "high":

                        e.Value = highPriImage;

                        break;

                    case "medium":

                        e.Value = mediumPriImage;

                        break;

                    case "low":

                        e.Value = lowPriImage;

                        break;

                }

            }



        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值