1. 程式人生 > >C# DataGridView下DataGridViewComboBoxColumn二級聯動

C# DataGridView下DataGridViewComboBoxColumn二級聯動

從數據 exc VC 9.png tac datarow private dataerror tab

效果:

技術分享圖片

代碼:

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

namespace DataGridViewDownComboBoxDemo
{
    public partial class Form1 : Form
    {
        //級聯的列  
        DataGridViewComboBoxColumn columns3 = null
; public Form1() { InitializeComponent(); this.dataGridView1.DataError += delegate (object sender, DataGridViewDataErrorEventArgs e) { }; } /// <summary> /// 綁定GRIDVIEW /// </summary> public void GetData() {
//第二列 DataGridViewComboBoxColumn columns2 = new DataGridViewComboBoxColumn(); columns2.Name = "col2"; columns2.DataPropertyName = "Type"; //從數據源綁定列,這一列就是那個值 columns2.HeaderText = "貨區"; string[] strTmp = new string[] { "貨區A", "貨區B", "貨區C" , "
貨區D", "貨區E", "貨區F" }; DgvCombobox(ref columns2, strTmp);//此方法是定義的關鍵 //第三列 columns3 = new DataGridViewComboBoxColumn(); columns3.Name = "col3"; columns3.HeaderText = "貨架"; //生成gridview數據源 DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("序號")); dt.Columns.Add(new DataColumn("物資名稱")); dt.Columns.Add(new DataColumn("物資編號")); for (int i = 0; i < 20; i++) { DataRow dr = dt.NewRow(); dr[0] = i+1; dr[1] = "戴爾筆記本"; dr[2] = "00000"+(i+1).ToString(); dt.Rows.Add(dr); } this.dataGridView1.DataSource = dt; //gridview添加列 dataGridView1.Columns.AddRange(new DataGridViewColumn[] { columns2, columns3 }); } /// <summary> /// 綁定第二列 /// </summary> /// <param name="column"></param> /// <param name="strTmp"></param> public void DgvCombobox(ref DataGridViewComboBoxColumn column, string[] strTmp) { DataTable dt = new DataTable(); DataColumn dc = new DataColumn("tmp", typeof(string)); dt.Columns.Add(dc); dt.Columns.Add(new DataColumn("ID", typeof(string))); DataRow dr; for (int i = 0; i < strTmp.Length; i++) { dr = dt.NewRow(); dr["tmp"] = strTmp[i]; dr["id"] = i; dt.Rows.Add(dr); } //為combobox綁定生成的表 column.DataSource = dt; //combobox列的數據源,綁定為生成的表 column.DisplayMember = "tmp";//要顯示的名稱,表的文字例 column.ValueMember = dt.Columns[1].ToString();//文字對應的值,此列將和columns2.DataPropertyName 屬性的值對應來顯示選中的值 } private void Form1_Load(object sender, EventArgs e) { this.GetData(); } //添加委托事件 private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (this.dataGridView1.CurrentCell.OwningColumn.Name == "col2") { ((ComboBox)e.Control).SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged); } } private void ComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (this.dataGridView1.CurrentCell.OwningColumn.Name == "col2") { string str = ((ComboBox)sender).Text.ToString(); //綁定第二個COMBOX DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("id")); dt.Columns.Add(new DataColumn("text")); for (int i = 0; i < 20; i++) { DataRow dr = dt.NewRow(); dr[0] = str + i; dr[1] = str + i; dt.Rows.Add(dr); } ((DataGridViewComboBoxCell)this.dataGridView1.CurrentRow.Cells[4]).DataSource = dt; ((DataGridViewComboBoxCell)this.dataGridView1.CurrentRow.Cells[4]).DisplayMember = "text"; ((DataGridViewComboBoxCell)this.dataGridView1.CurrentRow.Cells[4]).ValueMember = "id"; ((ComboBox)sender).SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged); } } } }

提示:此demo不需要鏈接數據庫,只要在自己的窗體上創建一個datagridview命名為datagridview1即可。

C# DataGridView下DataGridViewComboBoxColumn二級聯動