【发布时间】:2010-08-20 07:10:36
【问题描述】:
我有两个组合框。我在第一个组合框中插入一个值,现在我希望我的第二个组合框根据第一个组合框更新其值。我该怎么做?
【问题讨论】:
-
您使用的是哪种技术? WPF、Silverlight、winforms?
我有两个组合框。我在第一个组合框中插入一个值,现在我希望我的第二个组合框根据第一个组合框更新其值。我该怎么做?
【问题讨论】:
处理第一个ComboBox 的SelectedIndexChanged 事件,然后根据第一个ComboBox 的SelectedItem 值更新第二个组合框。
一个简单的例子(在检索 SelectedItem 时没有错误处理):
public partial class Form1 : Form
{
private string[] comboBox1Range = new[] { "A", "B", "C", "D" };
private string[] comboBox2RangeA = new[] { "A1", "A2", "A3", "A4" };
private string[] comboBox2RangeB = new[] { "B1", "B2", "B3", "B4" };
private string[] comboBox2RangeC = new[] { "C1", "C2", "C3", "C4" };
private string[] comboBox2RangeD = new[] { "D1", "D2", "D3", "D4" };
public Form1()
{
InitializeComponent();
comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
comboBox1.Items.AddRange(comboBox1Range);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedValue = comboBox1.SelectedItem as string;
switch (selectedValue)
{
case "A":
comboBox2.Items.Clear();
comboBox2.Items.AddRange(comboBox2RangeA);
break;
case "B":
comboBox2.Items.Clear();
comboBox2.Items.AddRange(comboBox2RangeB);
break;
case "C":
comboBox2.Items.Clear();
comboBox2.Items.AddRange(comboBox2RangeC);
break;
case "D":
comboBox2.Items.Clear();
comboBox2.Items.AddRange(comboBox2RangeD);
break;
}
}
}
【讨论】:
订阅第一个组合框的值更改事件并填充第二个:
combobox1.SelectedIndexChanged+= new EventHandler(ListBox1_SelectedIndexChanged);
private combobox1_SelectedIndexChanged(object sender, EventArgs e)
{
// do stuff with combobox2
}
或
combobox1.SelectedValueChanged += new EventHandler(ListBox1_SelectedValueChanged);
private combobox1_SelectedValueChanged(object sender, EventArgs e)
{
// do stuff with combobox2
}
人口:
combobox2.Items.Add(new object());
combobox2.Items.Add(new ListItem("caption", "value"));
// etc
查找现有项目:
var index = combobox2.FindStringExact(combobox1.SelectedText);
if (index != -1)
comobox2.SelectedItem = combobox2.Items[index];
【讨论】:
代码示例,带有两个组合框的 Winform 数据绑定到数据集中的两个表。 表“lstCountries”当前国家列表 表“lstState”列出所有国家/地区的所有州/省。
表 lstCountries(Int32 CountryID,string CountryName) 表 lstStates(Int32 StateID, Int32 CountryID, string StateName)
在这段代码中,我根据选定的 cboCountry 值填充 cboState 而两个组合下拉菜单都绑定到从数据库中获取的数据表。
// Load data from database (not shown)
// _dataSet.Tables["lstCountries"] datasource for cboCountry
// _dataSet.Tables["lstStates"] datasource for cboState
//
// cboCountry - comboDropDown - List on countries
// cboState = comboDropDown - List of states
// Use boolean bloading to prevent setting datasource for cboState when cboCountry is intially loaded.
void cboCountry_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cbo = (sender as ComboBox);
if (cbo.SelectedIndex > -1 && !bloading)
{
Int32 countryID = Convert.ToInt32(((System.Data.DataRowView)(cbo.SelectedItem)).Row.ItemArray[0].ToString());
cboState.Text = "";
DataView view = _dataSet.Tables["lstStates"].DefaultView;
view.RowFilter = string.Format("CountryID={0}", countryID);
DataTable table = view.ToTable();
cboState.DataSource = table;
cboState.SelectedIndex = -1;
}
【讨论】:
我想选择 Ders 这个 cmbDers 然后 cmbKonu 更新相关的 SQL,比如那个 Country-City。但我得到一个数据类型不匹配。什么问题?
表单加载 OleDbDataAdapter adp = new OleDbDataAdapter("select * from Ders", baglanti); 数据表 dt = 新数据表(); baglanti.Open(); adp.填充(dt); cmbDers.DataSource = dt; cmbDers.DisplayMember = "DersAd"; cmbDers.ValueMember = "DersID"; baglanti.Close();
private void cmbDers_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = new DataTable();
OleDbDataAdapter adp = new OleDbDataAdapter ("select * from Konu where DersID = '" +cmbDers.SelectedItem+"'",baglanti);
adp.Fill(dt);
cmbKonu.DataSource = dt;
cmbKonu.DisplayMember = "KonuAd";
cmbKonu.ValueMem`enter code here`ber = "KonuID";
baglanti.Close();`enter code here`
【讨论】: