我有2张桌子。 1个表-带[ID_house],[House_names]和外键[num_Area]的“房子”。 2个表-带[ID_area],[Area_names]的“ Area”。
我尝试从第一个组合框中使用SelectedValue,但从中获得空值。 可能是我在输入正确的ValueMember时犯了一个错误。
以下是两个带有ValueMembers的组合框的代码:
//1st combobox
string query1 = "SELECT House.num_Area, House_names FROM House";
OleDbCommand command1 = new OleDbCommand(query1, connection);
OleDbDataReader reader1 = command1.ExecuteReader();
while (reader1.Read())
{
cmbhouse.Items.Add(reader1["House_names"].ToString());
}
cmbhouse.ValueMember = "House.num_Area";
reader1.Close();
//2nd combobox
string query2 = "SELECT * FROM Area";
OleDbCommand command2 = new OleDbCommand(query2, connection);
OleDbDataReader reader2 = command2.ExecuteReader();
while (reader2.Read())
{
cmbarea.Items.Add(reader2["Area_names"].ToString());
}
cmbregion.ValueMember = "ID_area";
reader2.Close();
在这里,我尝试使用此代码填充第二个组合框:
if (cmbhouse.SelectedValue.ToString()!="")
{
int house_numArea = Convert.ToInt32(cmbhouse.SelectedValue.ToString());
string query2 = "SELECT * FROM Area, House WHERE Area.ID_area='" + house_numArea + "'";
OleDbCommand command2 = new OleDbCommand(query2, connection);
OleDbDataReader reader2 = command2.ExecuteReader();
while (reader2.Read())
{
cmbarea.Items.Add(reader2["Area_names"].ToString());
}
reader2.Close();
}
如果我在第一个组合框中选择任何房子,例如,我想在第二个组合框中仅显示该房子所在的区域。