效果:

C# DataGridView下DataGridViewComboBoxColumn二级联动-LMLPHP

代码:

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 = ; i < ; i++)
{
DataRow dr = dt.NewRow();
dr[] = i+;
dr[] = "戴尔笔记本";
dr[] = ""+(i+).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 = ; 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[].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 = ; i < ; i++)
{
DataRow dr = dt.NewRow();
dr[] = str + i;
dr[] = str + i;
dt.Rows.Add(dr);
}
((DataGridViewComboBoxCell)this.dataGridView1.CurrentRow.Cells[]).DataSource = dt;
((DataGridViewComboBoxCell)this.dataGridView1.CurrentRow.Cells[]).DisplayMember = "text";
((DataGridViewComboBoxCell)this.dataGridView1.CurrentRow.Cells[]).ValueMember = "id";
((ComboBox)sender).SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged); }
}
}
}

提示:此demo不需要链接数据库,只要在自己的窗体上创建一个datagridview命名为datagridview1即可。

    datagridview1的EditMode设为EditOnEnter可实现点击一次就出下拉列表。

05-11 13:51