首页 > 代码库 > winfrom

winfrom

省市联动  

 

private void biands()
{
string sql = "select AreaId, AreaName from TblArea where AreaPId=0";
SqlDataReader dr = SqlHelper.ExecuteReader(sql);
List<Area> list = new List<Area>();
while (dr.Read())
{
Area area = new Area();
area.AreaId = Convert.ToInt32(dr["AreaId"]);
area.AreaName = dr["AreaName"].ToString();
list.Add(area);
}
cbosheng.DisplayMember = "AreaName";
cbosheng.ValueMember = "AreaId";
cbosheng.DataSource = list;
}

private void cbosheng_SelectedIndexChanged(object sender, EventArgs e)
{
int id = Convert.ToInt32(cbosheng.SelectedValue.ToString());
string sql = "select AreaId, AreaName from TblArea where AreaPId=@id";
SqlDataReader dr = SqlHelper.ExecuteReader(sql,new SqlParameter("@id",id));
List<Area> list = new List<Area>();
while (dr.Read())
{
Area area = new Area();
area.AreaId = Convert.ToInt32(dr["AreaId"]);
area.AreaName = dr["AreaName"].ToString();
list.Add(area);
}

cboshi.DisplayMember = "AreaName";   //实际显示个客户看的内容
cboshi.ValueMember = "AreaId";   //里面存取的值     方便

private void biands()
{
string sql = "select AreaId, AreaName from TblArea where AreaPId=0";
SqlDataReader dr = SqlHelper.ExecuteReader(sql);
List<Area> list = new List<Area>();
while (dr.Read())
{
Area area = new Area();
area.AreaId = Convert.ToInt32(dr["AreaId"]);
area.AreaName = dr["AreaName"].ToString();
list.Add(area);
}
cbosheng.DisplayMember = "AreaName";
cbosheng.ValueMember = "AreaId";
cbosheng.DataSource = list;
}

private void cbosheng_SelectedIndexChanged(object sender, EventArgs e)
{
int id = Convert.ToInt32(cbosheng.SelectedValue.ToString());
string sql = "select AreaId, AreaName from TblArea where AreaPId=@id";
SqlDataReader dr = SqlHelper.ExecuteReader(sql,new SqlParameter("@id",id));
List<Area> list = new List<Area>();
while (dr.Read())
{
Area area = new Area();
area.AreaId = Convert.ToInt32(dr["AreaId"]);
area.AreaName = dr["AreaName"].ToString();
list.Add(area);
}

cboshi.DisplayMember = "AreaName";   //显示给客户看到的值
cboshi.ValueMember = "AreaId";          // 存在里面的值    方便comboBox控件中的SelectedValue属性拿到  
cboshi.DataSource = list;


在这里 我犯了一个错误 之前 comboBox.ValueMember  存的是AreaPId  导致 省市 一直不同步  

cbosheng.SelectedIndex;  索引  这样的话  会出现 以下情况

选择省的时候  市的下拉列表中 会出现数据库中选中的省的上一个省的市区 

 

winfrom