两个关联combobox控件数据绑定问题?(我没分了)
数据库:Province:PIDPName;City:CIDCNamePID目的:Combobox1显示PName字段,随其字段的变换,Combobox2中显示相应的城市...
数据库:Province:PID PName;City:CID CName PID
目的:Combobox1显示PName字段,随其字段的变换,Combobox2中显示相应的城市CName。
代码中有问题,希望能得到两组PID,PName。PName如下代码赋给FlightProvinceCom.Items;PID用于Combobox2的查询条件
代码如下:
public ForSaleTicksSearch()
{
InitializeComponent();
}
private void ForSaleTicksSearch_Load(object sender, EventArgs e)
{
try
{
string sql = "select ProvinceId,ProvinceName from ProvinceInfo";
SqlCommand command = new SqlCommand(sql, MyAirTicketConnection.MyCon);
MyAirTicketConnection.MyCon.Open();
SqlDataReader dataReader = command.ExecuteReader();
int provinceId = 0;
string provinceName = "";
while (dataReader.Read())
{
provinceId = (int)dataReader[0];
provinceName = (string)dataReader[1];
FlightProvinceCom.ValueMember = provinceId.ToString();
FlightProvinceCom.Items.Add(provinceName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message); //"操作失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error
}
finally
{
MyAirTicketConnection.MyCon.Close();
}
}
private void FlightProvinceCom_SelectedIndexChanged(object sender, EventArgs e)
{
FlightCityCom.Items.Clear();
try
{
string sql ="SELECT CityID, CityName, ProvinceId FROM CityInfo WHERE ProvinceId="+FlightProvinceCom.ValueMember;这里ValueMember怎么不随FlightProvinceCom.Text值变化?
SqlCommand command = new SqlCommand(sql, MyAirTicketConnection.MyCon);
MyAirTicketConnection.MyCon.Open();
SqlDataReader dataReader = command.ExecuteReader();
int cityID = 0;
string cityName = "";
while (dataReader.Read())
{
cityID = (int)dataReader[0];
cityName = (string)dataReader[1];
FlightCityCom.ValueMember = cityID.ToString();
FlightCityCom.Items.Add(cityName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message); //"操作失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error
}
finally
{
MyAirTicketConnection.MyCon.Close();
}
}
我是想把Province:PID PName的两组值都取出,PName显示在combobox1.Items中,PID能存在哪个组中,当combobox1.Text显示的变化时,也随此变化。这样我在填充combobox2查询时,就可以string sql ="SELECT CityID, CityName, ProvinceId FROM CityInfo WHERE ProvinceId= {0} "得到Combobox1.Text值下相应城市的combobox2.Items数据组。 展开
目的:Combobox1显示PName字段,随其字段的变换,Combobox2中显示相应的城市CName。
代码中有问题,希望能得到两组PID,PName。PName如下代码赋给FlightProvinceCom.Items;PID用于Combobox2的查询条件
代码如下:
public ForSaleTicksSearch()
{
InitializeComponent();
}
private void ForSaleTicksSearch_Load(object sender, EventArgs e)
{
try
{
string sql = "select ProvinceId,ProvinceName from ProvinceInfo";
SqlCommand command = new SqlCommand(sql, MyAirTicketConnection.MyCon);
MyAirTicketConnection.MyCon.Open();
SqlDataReader dataReader = command.ExecuteReader();
int provinceId = 0;
string provinceName = "";
while (dataReader.Read())
{
provinceId = (int)dataReader[0];
provinceName = (string)dataReader[1];
FlightProvinceCom.ValueMember = provinceId.ToString();
FlightProvinceCom.Items.Add(provinceName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message); //"操作失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error
}
finally
{
MyAirTicketConnection.MyCon.Close();
}
}
private void FlightProvinceCom_SelectedIndexChanged(object sender, EventArgs e)
{
FlightCityCom.Items.Clear();
try
{
string sql ="SELECT CityID, CityName, ProvinceId FROM CityInfo WHERE ProvinceId="+FlightProvinceCom.ValueMember;这里ValueMember怎么不随FlightProvinceCom.Text值变化?
SqlCommand command = new SqlCommand(sql, MyAirTicketConnection.MyCon);
MyAirTicketConnection.MyCon.Open();
SqlDataReader dataReader = command.ExecuteReader();
int cityID = 0;
string cityName = "";
while (dataReader.Read())
{
cityID = (int)dataReader[0];
cityName = (string)dataReader[1];
FlightCityCom.ValueMember = cityID.ToString();
FlightCityCom.Items.Add(cityName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message); //"操作失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error
}
finally
{
MyAirTicketConnection.MyCon.Close();
}
}
我是想把Province:PID PName的两组值都取出,PName显示在combobox1.Items中,PID能存在哪个组中,当combobox1.Text显示的变化时,也随此变化。这样我在填充combobox2查询时,就可以string sql ="SELECT CityID, CityName, ProvinceId FROM CityInfo WHERE ProvinceId= {0} "得到Combobox1.Text值下相应城市的combobox2.Items数据组。 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询