有人能帮忙写个二级联动吗?要从数据库调得那个。就来个大概。然后能给我讲懂了就行!感谢! 100

 我来答
zhangguyunbd
2016-03-15
知道答主
回答量:2
采纳率:0%
帮助的人:2287
展开全部
 省、市联动
private DataSet dsProvince = null;//省
 private DataSet dsCity = null;//市
 private DataView dvCity = null;
 public string provinceId = "";//省级编号
public string cityId = "";//市级编号
private string idStr = "";
 private string province;//省级名称
  private string city;
 private void CityControl_Load(object sender, EventArgs e)
        {
            lkpProvince.Visible = ProvinceShow;
            lkpCity.Visible = CityShow;
            lkpCounty.Visible = CountyShow;
            lkpProvince.Text = Province;
            provinceId = SetBh(1, "0", province);
            lkpCity.Text = City;
            cityId = SetBh(2, provinceId, city);
        }
/根据编号和父编号查询符合条件的一行,并返回这行的名字
 public DataRow SetDataRow(DataTable dt, string pid, string id, out string name)
        {
            name = "";
            DataView dvSet = dt.DefaultView;
            dvSet.RowFilter = " CITYID='" + id + "' and PARENTID='" + pid + "' ";
            DataTable dataTable = dvSet.ToTable();
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                name = dataTable.Rows[0]["CITY"] == null ? "" : dataTable.Rows[0]["CITY"].ToString();
                return dataTable.Rows[0];
            }
            return null;
        }
  //根据名字和父编号,返回本身编号
        private string SetBh(int index, string pid, string name)
        {
            DataTable dt = null;
            if (index == 1)
            {
                if (dsProvince != null && dsProvince.Tables != null && dsProvince.Tables[0] != null && dsProvince.Tables[0].Rows.Count > 0)
                {
                    dt = dsProvince.Tables[0];
                }
            }
            else if (index == 2)
            {
                if (dsCity != null && dsCity.Tables != null && dsCity.Tables[0] != null && dsCity.Tables[0].Rows.Count > 0)
                {
                    dt = dsCity.Tables[0];
                }
            }
            string bh = "";
            if (dt != null && dt.Rows.Count > 0)
            {
                if (pid != "" && name != "")
                {
                    DataView dvSet = dt.DefaultView;
                    dvSet.RowFilter = " CITY='" + name + "' and PARENTID='" + pid + "' ";
                    DataTable dataTable = dvSet.ToTable();
                    if (dataTable != null && dataTable.Rows.Count > 0)
                    {
                        bh = dataTable.Rows[0]["CITYID"] == null ? "" : dataTable.Rows[0]["CITYID"].ToString();
                    }
                }
            }
            return bh;
        }


     #region 省级
        private void lkpProvince_Leave(object sender, EventArgs e)
        {
            province = this.lkpProvince.Text + "";
            #region 第一步:确定省级状况
            if (province == "")//当未选择任何省份时
            {
                provinceId = "";
                province = "";
                cityId = "";
                city = "";
                countyId = "";
                county = "";
                lkpProvince.Text = "";
                lkpCity.Text = "";
                lkpCounty.Text = "";
                SetDorp(0, "", false, out idStr);
                return;
            }
            //判断当前省份在列表中是否存在
            DataRow[] prRow = dsProvince.Tables[0].Select(" CITY='" + province + "'");
            if (prRow != null && prRow.Length > 0)
            {
                province = prRow[0]["CITY"] + "";
                if ((provinceId != "" && provinceId != prRow[0]["CITYID"] + "") || provinceId == "")
                {
                    provinceId = prRow[0]["CITYID"] + "";
                }
                //当省文本框不为空,省ID不为空且下拉列表未选中任何行时
                if (province != "" && provinceId != "" && (lkpProvince.SelectedRow == null || lkpProvince.SelectedRow["CITYID"].ToString() == ""))
                {
                    this.lkpProvince.SelectedRow = SetDataRow(dsProvince.Tables[0], "0", provinceId, out province);
                }
            }
            else
            {
                provinceId = "";
                lkpCity.Table = dsCity.Tables[0];
            }
            #endregion

            #region 第二步:确定市级情况
            if (string.IsNullOrEmpty(provinceId) && (!string.IsNullOrEmpty(cityId)))
            {
                cityId = "";
                lkpCity.Text = "";
            }
            else if (!string.IsNullOrEmpty(provinceId))
            {
                lkpCity.Table = SetDorp(1, " PARENTID='" + provinceId + "'", true, out idStr);
                if (!string.IsNullOrEmpty(cityId))
                {
                    if (!(("," + idStr + ",").Contains(",'" + cityId + "',")))
                    {
                        cityId = "";
                        lkpCity.Text = "";
                    }
                    else
                    {
                        this.lkpCity.SelectedRow = SetDataRow(dsCity.Tables[0], provinceId, cityId, out city);
                    }
                }
            }
            #endregion
            if (province != "")
                lkpCity.Focus();
        }
        #endregion

        #region 市级
        private void lkpCity_Leave(object sender, EventArgs e)
        {
            #region 第一步:确定市级情况
            city = this.lkpCity.Text + "";
            if (province != "" && city == "")//当市级选择为空字符串时
            {
                cityId = "";
                countyId = "";
                county = "";
                return;
            }
            else if (province == "" && city == "")
            {
                province = "";
                provinceId = "";
                cityId = "";
                countyId = "";
                county = "";
                SetDorp(0, "", false, out idStr);
                return;
            }
            //判断当前选择市是否存在于列表
            DataRow[] crRow = null;
            if (!string.IsNullOrEmpty(provinceId))
            {
                crRow = dsCity.Tables[0].Select(" CITY='" + city + "' and PARENTID='" + provinceId + "' ");
            }
            else
            {
                crRow = dsCity.Tables[0].Select(" CITY='" + city + "'");
            }
            if (crRow != null && crRow.Length > 0)
            {
                city = crRow[0]["CITY"] + "";
                if ((cityId != "" && cityId != crRow[0]["CITYID"] + "") || cityId == "")
                {
                    cityId = crRow[0]["CITYID"] + "";
                    provinceId = crRow[0]["PARENTID"] + "";
                }
                //当省级ID不为空,市级ID不为空且市级列表未选择任何行时
                if (province != "" && provinceId != "" && city != "" && cityId != "" && (lkpCity.SelectedRow == null || lkpCity.SelectedRow["CITYID"].ToString() == ""))
                {
                    this.lkpCity.SelectedRow = SetDataRow(dsCity.Tables[0], provinceId, cityId, out city);
                }
            }
            else
            {
                cityId = "";
            }
            if (!string.IsNullOrEmpty(provinceId))
            {
                this.lkpCity.Table = SetDorp(1, " PARENTID='" + provinceId + "'", false, out idStr);
                if (!string.IsNullOrEmpty(cityId))
                {
                    this.lkpCity.SelectedRow = SetDataRow(dsCity.Tables[0], provinceId, cityId, out city);
                    this.lkpCity.Text = city;
                }
            }
            #endregion

            #region 第二步:确定省级情况
            if ((!string.IsNullOrEmpty(provinceId)))
            {
                this.lkpProvince.SelectedRow = SetDataRow(dsProvince.Tables[0], "0", provinceId, out province);
                this.lkpProvince.Text = province;
            }
            #endregion

        
          
        }
        #endregion
追问
能讲解么?
追答
这个

主键ID,名称,级别(省,市,县),父ID

讲解:
1.分别定义省集合、市集合且定义当前选中的省编号、市区编号以及名称变量
2.在加载方法CityControl_Load时从数据库中读取信息至各个集合以及变量
3.当选择好省信息离开控件lkpProvince_Leave时,根据省ID筛选市列表中应该存在于这个省的所有市信息
4.当选择好市信息离开控件kpCity_Leave时,两种情况
1).省已经选择,不错其他处理
2).省未选择,只是选择了市,则根据市对应的父ID推出省,自动选中该省
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式