一、如下comboBox1、comboBox2、comboBox3,原来这三个都是空的,

将数据库中的省份传递到comboBox1中

我的数据库有parent字段,根据市的parent找到省,根据县的找到市,所以下面的sql语句在省市县联动时,sql语句是一样的

二、代码

1、在Load方法中,加载省份(根据需求变换sql语句)

 private void Form1_Load(object sender, EventArgs e)
{ using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = con;
cmd.CommandText = string.Format("select * from test t where t.[district_level]=2");
int rows = cmd.ExecuteNonQuery();
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "name";
comboBox1.ValueMember="id";
}
} }

(1)、用sql命令查询要在comboBox中显示的值

(2)、将值给DataTable

(3)、DisplayMember 就是要显示的数据库中的字段:name

(4)、ValueMember:一般是数据库中的主键:id

2、进入comboBox1事件,根据省份,显示市

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{ if (comboBox1.SelectedIndex > -)
{
DataRowView drv = (DataRowView)comboBox1.SelectedItem;
string id = drv.Row["id"].ToString();//获得已绑定的选项的id using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = con;
cmd.CommandText = string.Format("select * from test t where t.[parent]='" + id + "'");
int rows = cmd.ExecuteNonQuery();
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
comboBox2.DataSource = dt;
comboBox2.DisplayMember = "name";
comboBox2.ValueMember = "id";
}
} } }

3、进入comboBox2事件,根据市,显示县

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{ if (comboBox2.SelectedIndex > -)
{
DataRowView drv = (DataRowView)comboBox2.SelectedItem;
string id = drv.Row["id"].ToString();//获得id using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = con;
cmd.CommandText = string.Format("select * from test t where t.[parent]='" + id + "'");
int rows = cmd.ExecuteNonQuery();
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
comboBox3.DataSource = dt;
comboBox3.DisplayMember = "name";
comboBox3.ValueMember = "id";
}
} } }

三、 结果:

四 、有的省没有县,县是空值时,联动会出现异常,上一次联动的县的值还在显示,

点击县时,因为县的下一级没有值,显示的还是上次联动的结果,其实它并没有处于选中状态,只是显示出来了

所以,处理一下,因为没有处于选中状态,所以SelectedIndex是-1

if(comboBox3.SelectedIndex!=)
{
comboBox3.Text = "";
}

五、其他:

1、如果一个页面有多条这样的省市县联动

把上面的步骤再来一遍即可,只是Load里需要注意,不能图省事把comboBox4直接加在comboBox1后,否则点下面的省份,上面的也会变

using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = con;
cmd.CommandText = string.Format("select * from test where t.[district_level]=2");
int rows = cmd.ExecuteNonQuery();
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "name";
comboBox1.ValueMember="id"; }
} using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = con;
cmd.CommandText = string.Format("select * from test t where t.[district_level]=2");
int rows = cmd.ExecuteNonQuery();
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt); comboBox4.DataSource = dt;
comboBox4.DisplayMember = "name";
comboBox4.ValueMember = "id";
}
}

2、把comboBox1的事件代码封装成一个类DBDao.cs,方法名为getAddress()

(1)sql语句用的一样的时候(上面说的,根据市的parent找到省,根据县的parent找到市),我把sql语句也封装了

public static void getAddress(ComboBox combo1, ComboBox combo2)
{
if (combo1.SelectedIndex > -)
{
DataRowView drv = (DataRowView)combo1.SelectedItem;
string id = drv.Row["id"].ToString();//获得id
using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = con;
cmd.CommandText = string.Format("select t.* from test t where t.[parent]='" + id + "'");
int rows = cmd.ExecuteNonQuery();
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
combo2.DataSource = dt;
combo2.DisplayMember = "name";
combo2.ValueMember = "id"; }
}
}
}

在comboBox1的点击事件中直接调用即可,

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DBDao.getAddress(comboBox1, comboBox2);
} private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
DBDao.getAddress(comboBox2, comboBox3);
}

(2)sql语句不同的时候,不要把sql语句封装(留着以后用)

public static void ComboBoxLoad(string sql, ComboBox combobox)
{
using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = con;
cmd.CommandText = string.Format(sql);
int rows = cmd.ExecuteNonQuery();
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt); combobox.DataSource = dt;
combobox.DisplayMember = "name";
combobox.ValueMember = "id"; }
} }

其他页面调用:

string sql="select * from test t where t.id='0302' order by t.code asc";
DBDao.ComboBoxLoad(sql,comboBox12);

参考:

https://zhidao.baidu.com/question/242563101.html?qbl=relate_question_2&word=c%20combobox%BC%D3%D4%D8%CA%FD%BE%DD&skiptype=2

C#winform省市县联动,以及有的县是空值时显示异常的处理的更多相关文章

  1. winform水平滚动条联动panel

    需求: 滚动滚动条时显示pnlBack里面的button 文本框里输入数字,改变每行显示的按钮数 源码如下: /// <summary> /// 窗体加载 /// </summary ...

  2. C# 、winform 添加皮肤后(IrisSkin2) label设置的颜色 无法显示

    C# .winform 添加皮肤后(IrisSkin2) label设置的颜色 无法显示 解决方法一:设置label的Tag属性值与skinEngine的DisableTag属性值相同即可.默认值是9 ...

  3. 百度地图-省市县联动加载地图 分类: Demo JavaScript 2015-04-26 13:08 530人阅读 评论(0) 收藏

    在平常项目中,我们会遇到这样的业务场景: 客户希望把自己的门店绘制在百度地图上,通过省.市.区的选择,然后加载不同区域下的店铺位置. 先看看效果图吧: 实现思路: 第一步:整理行政区域表: 要实现通过 ...

  4. 省市县联动dropdownlist

    下面就是在提交按钮的单击事件中填写代码(代码区)(前提是把省市县的数据库建好) protected void Page_Load(object sender, EventArgs e)         ...

  5. JavaScript 全国级省市县联动

    <div class="right_content clearfix"> <h3 class="common_title2">收货地址& ...

  6. C# Winform ProgressBar+Labe 联动显示进度

    private void btnCount_Click(object sender, EventArgs e) { label1.Visible=true; progressBar.Visible = ...

  7. C# 鼠标移动Winform窗体内或者panel容器内的控件 显示虚线/实现虚线框来确定位置

    C# 鼠标移动WinForm窗体或者panel容器内的控件 移动虚线/实现虚线框来确定位置 1.用到的方法介绍 今天,根据领导指示指导移动容器内的控件,生成虚线框,使用 ControlPaint.Dr ...

  8. winform中的状态栏,以及在状态栏目上显示时间

    1:在winform上添加状态栏,并且在状态栏目上多添加几个label. step1:拖一个StatusStrip到winform上,名字默认为statusStrip1.找到statusStrip1的 ...

  9. Winform中实现更改DevExpress的RadioGroup的选项时更改其他控件(TextEdit、ColorPickEdit)的值

    场景 Winform中实现读取xml配置文件并动态配置ZedGraph的RadioGroup的选项: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article ...

随机推荐

  1. Codeforces 527C Glass Carving

    vjudge 上题目链接:Glass Carving 题目大意: 一块 w * h 的玻璃,对其进行 n 次切割,每次切割都是垂直或者水平的,输出每次切割后最大单块玻璃的面积: 用两个 set 存储每 ...

  2. arr.sort()排序方法

    <!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8&qu ...

  3. Android控件之EditText(输入文本框控件)

    一.EditText控件概述 EditText是一个非常重要的组件,可以说他是用户和Android应用进行数据传输窗口  有了他就等于有了一扇和Android应用传输的门,通过他用户可以把数据传输给A ...

  4. Kafka 消息存储及检索(作者:杜亦舒)

    Kafka 消息存储及检索 原创 2016-02-29 杜亦舒 性能与架构 Kafka是一个分布式的消息队列系统,消息存储在集群服务器的硬盘Kafka中可以创建多个消息队列,称为topic,消息的生产 ...

  5. phalcon: tasks MainTask.php命令行工具

    phalcon: tasks MainTask.php命令行工具 tasks MainTask.php 一般用来做计划任务,处理比较复杂的大型的数据.然后其他功能或程序才能更简单的读取这些复杂的数据. ...

  6. Falcon:三代reads比对组装工具箱

    主页:github: PacificBiosciences/FALCON 简介 Falcon是一组通过快速比对长reads,从而来consensus和组装的工具. Falcon工具包是一组简单的代码集 ...

  7. x^y=(x&~y)|(~x&y)证明

    我见过最棒的证明是文氏图:(首先要知道二元布尔代数是集合的特殊情况,所以把X和Y当作两个集合,结论成立,那么在二元布尔代数里面也成立.)左边的圈是X,右边的圈是Y.如果是OR 也就是取或,中间的白色的 ...

  8. as的Enter_Frame与Timer

    As3中的Timer和Event.EnterFrame是有明显的区别的. Evnet.EnterFrame是定时间隔多少时间出发.如果执行时间比间隔时间长,则会间隔执行时间这么久. 举个例子: Fla ...

  9. program testy data

    做项目得用数据吧,拿去.... 1.Data.gov搜索   美国政府去年承诺使所有政府数据都能在网上免费获得.这个网站是第一阶段,作为一个门户网站,囊括了从气候到犯罪的一切惊人的信息.   2. 美 ...

  10. 获取本机 Android 默认sha1 秘钥

    获取本机 Android 默认sha1 秘钥: 以Windows操作系统为例,打开CMD,运行以下指令将得到所有默认秘钥. keytool -list -v -keystore C:\Users\pa ...