【问题标题】:Cascade combobox in datagridview not working properlydatagridview 中的级联组合框无法正常工作
【发布时间】:2013-11-27 06:26:33
【问题描述】:

在 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;
} 

【问题讨论】:

    标签: c# winforms datagridview combobox


    【解决方案1】:

    我试过这个:

    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.
            }
    

    然后是表格组合框事件的选择变化:

    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);
    
            }
    

    【讨论】:

    • 你是DataGridView里面的组合框吗?
    • 是否可以分享屏幕截图或designer.cs,以便我可以看到两个组合框
    • 如代码所示,您使用的是简单的ComboBox 而不是DataGridViewComboBoxColumn
    • 你现在可以查看图片了。我已经编辑了答案
    • 你误解了这个问题,ComboBox 应该在每一行中重复。我的意思是说在选择一行的组合框后,应该使用相同的选项创建一个新行
    猜你喜欢
    • 2012-05-16
    • 2015-10-23
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 2017-01-04
    • 2020-04-23
    相关资源
    最近更新 更多