c#之combox下列列表框與datagridview及datagridviewtextboxcolumn關聯顯示

wisdomone1發表於2012-02-22
實現功能:
   1,在下拉選單框新增項
         2,在文字框顯示下拉選單框的不同項
         3,顯示datagridview的列表列標題為下拉選單框不同項的內容

程式碼:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace learncomboxanddatagridview
{
    public partial class Form1 : Form
    {

        private System.Windows.Forms.ComboBox comboBox1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.DataGridViewTextBoxColumn col1;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //items屬性的add方法
            comboBox1.Name = "cmb1";
            comboBox1.Items.Add("正常賬戶");
            comboBox1.Items.Add("睡眠賬戶");
            comboBox1.Items.Add("久懸賬戶");
            comboBox1.Items.Add("轉營業處收入賬戶");

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //選擇不同的下列列表框內容在文字框顯示對應內容
            //items屬性
            textBox1.Text = (string)comboBox1.Items[comboBox1.SelectedIndex];
            col1.HeaderText = textBox1.Text;
        }
    }
}



private void InitializeComponent()
        {
            //略去部分不重要內容,此方法程式碼為窗體設計器自動生成
            // dataGridView1
            //
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.col1});
            this.dataGridView1.Location = new System.Drawing.Point(22, 245);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.RowTemplate.Height = 23;
            this.dataGridView1.Size = new System.Drawing.Size(240, 150);
            this.dataGridView1.TabIndex = 2;
            //
            // col1
            //
            this.col1.HeaderText = "colheadertext";
            this.col1.Name = "col1";
            //
           
        }


時間有限,暫且寫這點

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9240380/viewspace-716922/,如需轉載,請註明出處,否則將追究法律責任。

相關文章