C#winform省市县联动,以及有的县是空值时显示异常的处理
一、如下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省市县联动,以及有的县是空值时显示异常的处理的更多相关文章
- winform水平滚动条联动panel
需求: 滚动滚动条时显示pnlBack里面的button 文本框里输入数字,改变每行显示的按钮数 源码如下: /// <summary> /// 窗体加载 /// </summary ...
- C# 、winform 添加皮肤后(IrisSkin2) label设置的颜色 无法显示
C# .winform 添加皮肤后(IrisSkin2) label设置的颜色 无法显示 解决方法一:设置label的Tag属性值与skinEngine的DisableTag属性值相同即可.默认值是9 ...
- 百度地图-省市县联动加载地图 分类: Demo JavaScript 2015-04-26 13:08 530人阅读 评论(0) 收藏
在平常项目中,我们会遇到这样的业务场景: 客户希望把自己的门店绘制在百度地图上,通过省.市.区的选择,然后加载不同区域下的店铺位置. 先看看效果图吧: 实现思路: 第一步:整理行政区域表: 要实现通过 ...
- 省市县联动dropdownlist
下面就是在提交按钮的单击事件中填写代码(代码区)(前提是把省市县的数据库建好) protected void Page_Load(object sender, EventArgs e) ...
- JavaScript 全国级省市县联动
<div class="right_content clearfix"> <h3 class="common_title2">收货地址& ...
- C# Winform ProgressBar+Labe 联动显示进度
private void btnCount_Click(object sender, EventArgs e) { label1.Visible=true; progressBar.Visible = ...
- C# 鼠标移动Winform窗体内或者panel容器内的控件 显示虚线/实现虚线框来确定位置
C# 鼠标移动WinForm窗体或者panel容器内的控件 移动虚线/实现虚线框来确定位置 1.用到的方法介绍 今天,根据领导指示指导移动容器内的控件,生成虚线框,使用 ControlPaint.Dr ...
- winform中的状态栏,以及在状态栏目上显示时间
1:在winform上添加状态栏,并且在状态栏目上多添加几个label. step1:拖一个StatusStrip到winform上,名字默认为statusStrip1.找到statusStrip1的 ...
- Winform中实现更改DevExpress的RadioGroup的选项时更改其他控件(TextEdit、ColorPickEdit)的值
场景 Winform中实现读取xml配置文件并动态配置ZedGraph的RadioGroup的选项: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article ...
随机推荐
- java实现将资源文件转化成sql语句导入数据库
文档结构
- [转]c++面向对象基础
from: here 1. C++面向对象程序设计的重要概念 早期革命影片里有这样一个角色,他说:“我是党代表,我代表党,我就是党.”后来他给同志们带来了灾难. 会用C++的程序员一定懂得面向对象程序 ...
- OpenGL的glViewport视口变换函数详解[转]
调用glViewPort函数来决定视见区域,告诉OpenGL应把渲染之后的图形绘制在窗体的哪个部位.当视见区域是整个窗体时,OpenGL将把渲染结果绘制到整个窗口. void glViewPort(G ...
- java 零碎1
1. java 程序的入口必须是 static 类型的,接口中不允许有 static , 而且接口中的方法必须是public. 2. java 回收主要针对的是堆区的回收. 3. java.exe 是 ...
- Machine Learning for hackers读书笔记(三)分类:垃圾邮件过滤
#定义函数,打开每一个文件,找到空行,将空行后的文本返回为一个字符串向量,该向量只有一个元素,就是空行之后的所有文本拼接之后的字符串 #很多邮件都包含了非ASCII字符,因此设为latin1就可以读取 ...
- Nginx内置常用变量
nginx用到的全局变量 $arg_PARAMETER #这个变量包含GET请求中,如果有变量PARAMETER时的值. $args #这个变量等于请求行中(GET请求)的参数,例如foo=123&a ...
- sealed(C# 参考)
sealed 修饰符可以应用于类.实例方法和属性.密封类不能被继承.密封方法会重写基类中的方法,但其本身不能在任何派生类中进一步重写.当应用于方法或属性时,sealed 修饰符必须始终与 overri ...
- Junit4入门
eclipse自带junit包,可右键直接新建junit类 静态引入:import static org.junit.Assert.* assert.*是类,静态引入会引入assert里的所有静态方法 ...
- Simplify Path [LeetCode]
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- 《JavaScript权威指南》读书笔记(四)
日期:2015-12-06 事件传播:1.捕捉阶段2.运行阶段3.起泡阶段cookie和客户端持久性::HTML5引入了web应用缓存.LocalStorage.SessionStorage:使用XM ...