码迷,mamicode.com
首页 > Windows程序 > 详细

winfrom

时间:2014-09-10 23:46:01      阅读:367      评论:0      收藏:0      [点我收藏+]

标签:os   ar   数据   sp   on   c   ad   new   数据库   

省市联动  

 

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

标签:os   ar   数据   sp   on   c   ad   new   数据库   

原文地址:http://www.cnblogs.com/GodLin/p/3965273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!