Datagridview Multiple Combobox

时间:2012-01-03 07:44:40

标签: c# gridview combobox datagridviewcombobox

我有一个名为GridView1的datagridview,它有两个列,每个都是组合框,我需要能够根据第一个组合框更改其中一个组合框中的项目。组合框是动态创建的,值与单个数据集绑定。最初,当我加载表单时,每件事情都很完美,但是当我更改第一个组合框的值时,第二个组合框的值不会改变。我尝试使用gridview的 EditingControlShowing 事件,然后将 SelectedIndexChanged 应用于组合框,但仍然无法弄明白。

网格视图Combox

DataGridViewComboBoxColumn seccol = new DataGridViewComboBoxColumn();
                    seccol.DataSource = semch.Tables["secall"];
                    seccol.Name = "SSSS";
                    seccol.DisplayMember = "SSSSNAME";
                    seccol.ValueMember = "SSSSID";
                    seccol.HeaderText = "SSSS";
                    seccol.DataPropertyName = "SSSSID";
                    seccol.DefaultCellStyle.Font = new Font("Microsoft Sans Serif", 10, FontStyle.Bold);
                    studpromo_gv.Columns.Add(seccol);
                    studpromo_gv.Columns["SEC"].DisplayIndex = 14;
                    studpromo_gv.Columns[14].HeaderCell.Style.BackColor = Color.LightSeaGreen;

EditingControlShowing事件

ComboBox cb = e.Control as ComboBox;
            if (cb != null)
            {
                cb.SelectedIndexChanged -= new EventHandler(cb_SelectedIndexChanged);
                cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
            }

更新代码

if (studpromo_gv.CurrentCell.ColumnIndex == 13)
            {
                ComboBox cmbBox = (ComboBox)sender;
                //int semx = Convert.ToInt32(cmbBox.SelectedValue);
                int semy = studpromo_gv.CurrentRow.Index;
                if (cmbBox != null)
                {
                    try
                    {
                        using (MySqlConnection conn = new MySqlConnection(MySQLconnection))
                        {
                            conn.Open();
                            MySqlDataAdapter secgvda = new MySqlDataAdapter("SELECT ID,NAME FROM STABLE WHERE SID='" + cmbBox.SelectedValue + "'", conn);
                            DataSet semch = new DataSet();
                            secgvda.Fill(semch, "secall");

                            (studpromo_gv[14, semy] as DataGridViewComboBoxCell).DataSource = semch.Tables["secall"];
                            (studpromo_gv[14, semy] as DataGridViewComboBoxCell).DisplayMember = "NAME";
                            (studpromo_gv[14, semy] as DataGridViewComboBoxCell).ValueMember = "ID";

                            conn.Close();
                            conn.Dispose();
                        }
                    }
                    catch (Exception)
                    {
                        //Some Statements
                    }

1 个答案:

答案 0 :(得分:0)

由于您要分配不同的DataSource,您可以选择将其添加到第二个DataGridViewComboBox列本身或该特定单元格,

因为您可以知道RowIndex和ColumnIndex,所以您可以根据所选的第一个值设置不同的DataSource

(dataGridView1[0,0] as DataGridViewComboBoxCell).DataSource = list of states

修改

由于你需要选择第一个项目,你可以添加它应该可以正常工作

(studpromo_gv[14, semy] as DataGridViewComboBoxCell).Value = (studpromo_gv[14, semy] as DataGridViewComboBoxCell).Items[0]