按组合框项目值删除组合框项目
Remove combobox item by combobox item value
我有两个组合框。我正在填充两个这样的组合框。组合框 1 和 2 的名称是 cmbpartyName 和 cmbprefPT2。
SqlDataReader reader = new VotingOP().getPartyNamesToCombo();
while (reader.Read())
{
cmbPartyName.Items.Add(new MyComboItem() { MyItemID = Convert.ToInt32(reader["partyID"]), MyItemName = reader["partyName"].ToString() });
}
reader.Close();
cmbPartyName.ValueMember = "MyItemID";
cmbPartyName.DisplayMember = "MyItemName";
SqlDataReader reader01 = new VotingOP().getPartyNamesToCombo();
while (reader01.Read())
{
cmbPrefPT2.Items.Add(new MyComboItem() { MyItemID = Convert.ToInt32(reader01["partyID"]), MyItemName = reader01["partyName"].ToString() });
}
reader01.Close();
cmbPrefPT2.ValueMember = "MyItemID";
cmbPrefPT2.DisplayMember = "MyItemName";
在这里,我在其选定的索引更改事件中从 combobox1 中选择一个值,如下所示。
private void cmbPartyName_SelectedIndexChanged(object sender, EventArgs e)
{
MyComboItem selectedItem = (MyComboItem)cmbPartyName.SelectedItem as MyComboItem;
if (selectedItem != null)
{
MessageBox.Show(String.Format("You've just selected the '{0}' partyID with the Name {1}",selectedItem.MyItemID,selectedItem.MyItemName));
}
}
由于两个组合框具有相同的值,我如何从第二个组合框 cmbPrefPT2 中删除第一个组合框 cmbparty 的选定值?
您可以让您的 MyComboItem 实现 IEquatable
public class MyComboItem : IEquatable<MyComboItem>
{
public int MyItemId { get; set; }
public string MyItemName { get; set; }
public bool Equals(MyComboItem other)
{
return MyItemId == other.MyItemId;
}
}
然后你可以在事件处理程序中调用第二个组合框的remove方法传递第一个组合框的selecteditem
MyComboItem selectedItem = (MyComboItem)cmbPartyName.SelectedItem as MyComboItem;
if (selectedItem != null)
{
cmbPrefPT2.Items.Remove(cmbPartyName.SelectedItem);
}
我有两个组合框。我正在填充两个这样的组合框。组合框 1 和 2 的名称是 cmbpartyName 和 cmbprefPT2。
SqlDataReader reader = new VotingOP().getPartyNamesToCombo();
while (reader.Read())
{
cmbPartyName.Items.Add(new MyComboItem() { MyItemID = Convert.ToInt32(reader["partyID"]), MyItemName = reader["partyName"].ToString() });
}
reader.Close();
cmbPartyName.ValueMember = "MyItemID";
cmbPartyName.DisplayMember = "MyItemName";
SqlDataReader reader01 = new VotingOP().getPartyNamesToCombo();
while (reader01.Read())
{
cmbPrefPT2.Items.Add(new MyComboItem() { MyItemID = Convert.ToInt32(reader01["partyID"]), MyItemName = reader01["partyName"].ToString() });
}
reader01.Close();
cmbPrefPT2.ValueMember = "MyItemID";
cmbPrefPT2.DisplayMember = "MyItemName";
在这里,我在其选定的索引更改事件中从 combobox1 中选择一个值,如下所示。
private void cmbPartyName_SelectedIndexChanged(object sender, EventArgs e)
{
MyComboItem selectedItem = (MyComboItem)cmbPartyName.SelectedItem as MyComboItem;
if (selectedItem != null)
{
MessageBox.Show(String.Format("You've just selected the '{0}' partyID with the Name {1}",selectedItem.MyItemID,selectedItem.MyItemName));
}
}
由于两个组合框具有相同的值,我如何从第二个组合框 cmbPrefPT2 中删除第一个组合框 cmbparty 的选定值?
您可以让您的 MyComboItem 实现 IEquatable
public class MyComboItem : IEquatable<MyComboItem>
{
public int MyItemId { get; set; }
public string MyItemName { get; set; }
public bool Equals(MyComboItem other)
{
return MyItemId == other.MyItemId;
}
}
然后你可以在事件处理程序中调用第二个组合框的remove方法传递第一个组合框的selecteditem
MyComboItem selectedItem = (MyComboItem)cmbPartyName.SelectedItem as MyComboItem;
if (selectedItem != null)
{
cmbPrefPT2.Items.Remove(cmbPartyName.SelectedItem);
}