DataGridView儲存格代碼控制

來源:互聯網
上載者:User
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;
                }
            }

        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.