数据网格视图多重组合框
本文关键字:重组 组合 视图 数据网 网格 数据 | 更新日期: 2023-09-27 18:19:13
我有一个名为GridView1的datagridview,它有两个列,每个列都是组合框,我需要能够根据第一个组合框改变其中的项目。组合框是动态创建的,值与单个数据集绑定。最初,当我加载表单一切都很完美,但当我改变第一个组合框的值第二个组合框值不改变。我尝试了 editingcontrolshow gridview事件,然后应用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;
<<p> EditingControlShowing事件/strong> ComboBox cb = e.Control as ComboBox;
if (cb != null)
{
cb.SelectedIndexChanged -= new EventHandler(cb_SelectedIndexChanged);
cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
}
<<p> 更新代码/strong> 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
}
既然你想要分配一个不同的DataSource
,你可以选择将它添加到第二个DataGridViewComboBox列本身或特定的单元格,
因为你知道RowIndex和ColumnIndex,你可以很好地根据第一个选择的值设置不同的数据源
(dataGridView1[0,0] as DataGridViewComboBoxCell).DataSource = list of states
编辑
因为你需要选择第一项,你可以添加这个,这应该很好
(studpromo_gv[14, semy] as DataGridViewComboBoxCell).Value = (studpromo_gv[14, semy] as DataGridViewComboBoxCell).Items[0]