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 ...
随机推荐
- Linux命令之nslookup
http://www.computerhope.com/unix/unslooku.htm About nslookup The nslookup command is used to query i ...
- Poco C++——HTTP的post请求和get请求
两种请求都需要包含头文件: #include <iostream> #include <string> #include "Poco/Net/HTTPClientSe ...
- [转]c++面向对象基础
from: here 1. C++面向对象程序设计的重要概念 早期革命影片里有这样一个角色,他说:“我是党代表,我代表党,我就是党.”后来他给同志们带来了灾难. 会用C++的程序员一定懂得面向对象程序 ...
- Java Date与SimpleDateFormat
最近在弄一些涉及到时间处理的项目.本来自己写了一个时间转换函数,虽然能用但是过于麻烦而且不够规范,于是学习了下java自带的时间处理的类. public class Timechg { public ...
- centos下vsftpd安装与配置
1.已经配置好可以上网了,所以即可通过yum install vsftpd安装啦.如果不能上网则可以通过dvd2.iso工具集rpm方式进行安装. 2.安装好后,默认本地可以通过匿名用户登录,但是其它 ...
- SVN操作手册
目 录 第1章 简介 1 第2章 SVN服务端 2 2.1 安装VisualSVN 2 2.2 VisualSVN服务 3 2.3 版本库 4 2.3.1 创建版本库 ...
- win8style布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...
- TWaver HTML5 (2D)----数据元素
概述 数据元素是数据模型的基本要素,用于描述图形网元,业务网元,或者纯数据.TWaver HTML5中所有数据元素都继承自twaver.Data.为不同功能的需求,预定义了三类数据类型:twaver. ...
- robotframework笔记4
机器人框架概述 机器人框架是一个通用的开源的自动化测试框架 验收测试和验收测试驱动开发(ATDD).它具有易于使用的表格的测试数据 语法采用关键字驱动 测试方法.其检测能力可以通过测试库 用Pyt ...
- easyui的基本用法
之前有用过extjs,最近发现easyui和fineui和extjs比较类似,并且稍微简单一点,所以考虑使用. 以下是项目中的具体简单应用 function callback2d(data) {//d ...