我有 2 个名为cBTeam1
( cBTeam2
winForm & C#) 的组合框都从同一个数据库表绑定。
如果一个人从中选择一个团队cBTeam1
,我希望这个选定的团队不显示在 中cBTeam2
。
private void bindComboBox()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
string queryTeam1 = "SELECT * FROM Teams ORDER BY Team_name";
SqlCommand cmd = new SqlCommand(queryTeam1, con);
adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds, "Teams");
this.cBoxTeam1.SelectedIndexChanged -= new EventHandler(this.cBoxTeam1_SelectedIndexChanged);
cBoxTeam1.DataSource = ds.Tables["Teams"];
//if(cBoxTeam1.SelectedIndex
cBoxTeam1.DisplayMember = "Team_name";
cBoxTeam1.SelectedIndex = -1;
cBoxTeam1.ValueMember = "team_id";
this.cBoxTeam1.SelectedIndexChanged += new EventHandler(this.cBoxTeam1_SelectedIndexChanged);
}
这是cBoxTeam2
事件处理程序的代码cBoxTeam2_SelectedIndexChanged
private void cBoxTeam2_SelectedIndexChanged(object sender, EventArgs e)
{
if (cBoxTeam1.SelectedIndex == cBoxTeam2.SelectedIndex)
{
MessageBox.Show("You already selected " + cBoxTeam2.Text);
}
team2_id = Int32.Parse(cBoxTeam2.SelectedValue.ToString());
}
例如cBoxTeam1
显示 3 个值,即英格兰、印度、澳大利亚。如果我选择印度,选择印度后不会显示在cBoxTeam2
组合框中