一、如下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. thinkphp3.2+PHPExcel导出查询数据到excel表格的实例

    首先下载PHPExcel插件,我们需要把PHPExcel.php和PHPExcel文件夹放到D:\XAMPP\htdocs\fsxb\ThinkPHP\Library\Vendor\PHPExcel目 ...

  2. JavaSE复习_9 集合框架复习

    △列表迭代器也是不支持在迭代的时候添加元素的,只是列表迭代器自己定义了增删的方法而已.迭代器可以看成实在两个元素之间的指针,每当调用next就跳过一个元素并返回刚刚跳过的元素. △HashTable不 ...

  3. NSString / NSData / char* 类型之间的转换

    转自网络: NSString / NSData / char* 类型之间的转换 1. NSString转化为UNICODE String: (NSString*)fname = @“Test”; ch ...

  4. C++ typedef 四个用途

    第一.四个用途 用途一: 定义一种类型的别名,而不只是简单的宏替换.可以用作同时声明指针型的多个对象.比如:char* pa, pb; // 这多数不符合我们的意图,它只声明了一个指向字符变量的指针, ...

  5. datagrid实现单行的选择、取消

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  6. Java中for循环遍历List的两种方法

    我们平常使用的方法: List<WebElement> element = driver.findElements(By.tagName("input"));      ...

  7. spring 编程式事务管理和声明式事务管理

    编程式事务管理 Spring 的编程式事务管理概述 在 Spring 出现以前,编程式事务管理对基于 POJO 的应用来说是唯一选择.用过 Hibernate 的人都知道,我们需要在代码中显式调用be ...

  8. 【MRPT】【icp-slam-live】Vs2013+ cmake3.6.1 + mrpt1.4.0+opencv2.9.4+wxWidget3.0.2环境配置

    Win10下Vs2013 + cmake3.6.1 + mrpt1.4.0+opencv2.9.4+wxWidget3.1.0环境配置 所接触过的最令我崩溃的环境配置.之前没有考虑到vs2013 20 ...

  9. Django缓存优化之redis

    Redis 概述 Redis 是一个开源的Inmemory key-value 存储系统,性能高,很大程度上补偿了 memcached 的不足.支持多种存储类型,包括 string, list, se ...

  10. RequireJS加载ArcGIS API for JavaScript

    1.在main.js中配置ArcGIS API for JavaScript require.config({ paths : { //arcgisJS "esri": " ...