C# DataGridView下DataGridViewComboBoxColumn二级联动
标签:ini his names show 提示 drawing header 技术分享 head
效果:
代码:
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) { };
}
///
/// 绑定GRIDVIEW
///
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 });
}
///
/// 绑定第二列
///
///
///
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 )
{
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二级联动
标签:ini his names show 提示 drawing header 技术分享 head
原文地址:https://www.cnblogs.com/swjian/p/9082454.html
评论