我在Windows窗体中实现级联ComboBox
时遇到困难。我有DataSet
个DataTable
。现在,我有一个DataGridView
控件,其中有两个ComboBox
:
ComboBox
将显示所有DataTable
个名称DataTable
问题是级联不能正常工作。完整代码如下:
DataSet ds = new DataSet();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ds.Tables.Add(GetTable1());
ds.Tables.Add(GetTable2());
IList<string> lstTables = ds.Tables.OfType<DataTable>().Select(dt => dt.TableName).ToList();
dgvColumn1.DataSource = lstTables;
dgvColumn1.ValueType = typeof(string);
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if ((dataGridView1.CurrentCell != null)
&& (dataGridView1.CurrentCell.ColumnIndex == 0))
{
DataGridViewComboBoxColumn cboColumn = (DataGridViewComboBoxColumn)dataGridView1.Columns[1];
if (ds.Tables[dataGridView1.CurrentCell.FormattedValue.ToString()].Columns.Count >0)
{
IList<string> lstColumn = ds.Tables[dataGridView1.CurrentCell.FormattedValue.ToString()].Columns.Cast<DataColumn>().Select(x => x.ColumnName).ToList();
cboColumn.DataSource = lstColumn;
cboColumn.ValueType = typeof(string);
}
else
{
cboColumn.DataSource = null;
}
}
}
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
}
DataTable GetTable1()
{
DataTable dt1 = new DataTable("table1");
dt1.Columns.Add("t1Col1");
dt1.Columns.Add("t1Col2");
dt1.Columns.Add("t1Col3");
dt1.Columns.Add("t1Col4");
return dt1;
}
DataTable GetTable2()
{
DataTable dt2 = new DataTable("table2");
dt2.Columns.Add("t2Col1");
dt2.Columns.Add("t2Col2");
dt2.Columns.Add("t2Col3");
dt2.Columns.Add("t2Col4");
return dt2;
}
答案 0 :(得分:0)
我试过这个:
private void Form1_Load_1(object sender, EventArgs e)
{
ds.Tables.Add(GetTable1());
ds.Tables.Add(GetTable2());
IList<string> lstTables = ds.Tables.OfType<DataTable>().Select(dt => dt.TableName).ToList();
dataGridView1.DataSource = lstTables;
comboBox1.DataSource = lstTables; //Setting the tables datasource for the comboBox.
}
然后选择更改表comboBox事件:
private void comboBox1_SelectedIndexChanged(object sender,EventArgs e) {
var obj = sender as ComboBox;
string s = obj.SelectedItem.ToString();
var y= (from x in ds.Tables.OfType<DataTable>() where x.TableName == s select x).ToList();
if (ds.Tables.Count > 0)
{
IList<string> lstCol = new List<string>();
for (int i = 0; i < y[0].Columns.Count; i++)
{
// Filling the list of columns according to the selected table
lstCol.Add(y[0].Columns[i].ToString());
}
// Setting dataSource for the column ComboBox
comboBox2.DataSource = lstCol;
//comboBox2.ValueType = typeof(string);
}
else
{
comboBox2.DataSource = null;
}
}
Designer.cs
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.comboBox2 = new System.Windows.Forms.ComboBox();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(26, 72);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(246, 150);
this.dataGridView1.TabIndex = 0;
this.dataGridView1.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellValueChanged_1);
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(28, 74);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 21);
this.comboBox1.TabIndex = 1;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// comboBox2
//
this.comboBox2.FormattingEnabled = true;
this.comboBox2.Location = new System.Drawing.Point(148, 74);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(121, 21);
this.comboBox2.TabIndex = 2;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.comboBox2);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load_1);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}