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"; // }