C# winform 选择项 省市连动
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; namespace 省市联动
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ //加载省份信息到第一个ComboBox
LoadProvince(); //设置两个下拉菜单的默认值为“请选择”
comboBox1.SelectedIndex = ;
comboBox2.SelectedIndex = ; }
private void LoadProvince()
{ string sql = "select * from TblArea where AreaPid=0";
using (SqlDataReader reader = SqlHelper.ExecuteReader(sql))
{
if (reader.HasRows)
{
while (reader.Read())
{
ProvinceItem item = new ProvinceItem();
item.AreaId = reader.GetInt32();
item.AreaName = reader.GetString();
item.AreaPid = reader.GetInt32();
comboBox1.Items.Add(item);
}
}
}
//为ComboBox 增加一个“请选择”
ProvinceItem itemDefault = new ProvinceItem();
itemDefault.AreaId = -;
itemDefault.AreaName = "请选择";
comboBox1.Items.Insert(, itemDefault); } //下拉菜单的选择项改变事件
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//获取当前用户选择的项
if (comboBox1.SelectedIndex > )
{
//加载第二个下拉菜单,数据来源:根据第一个下拉菜单用户选择项的AreaId来查询该项的所有子项。(子项:指的就是当前选中省份的下的直接城市) //获取当前选中项的id
ProvinceItem item = comboBox1.SelectedItem as ProvinceItem;
int areaId = item.AreaId;
LoadCity(areaId);
}
} private void LoadCity(int areaId)
{
comboBox2.Items.Clear();
string sql = "select * from TblArea where areaPId=@aid";
using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, new SqlParameter("@aid", areaId)))
{
if (reader.HasRows)
{
while (reader.Read())
{
ProvinceItem item = new ProvinceItem();
item.AreaId = reader.GetInt32();
item.AreaName = reader.GetString();
item.AreaPid = reader.GetInt32();
comboBox2.Items.Add(item);
}
}
} //也加一个【请选择】
ProvinceItem itemDefault = new ProvinceItem();
itemDefault.AreaId = -;
itemDefault.AreaName = "请选择";
comboBox2.Items.Insert(, itemDefault);
comboBox2.SelectedIndex = ;
}
}
public class ProvinceItem
{
public int AreaId { get; set; }
public string AreaName { get; set; }
public int AreaPid { get; set; }
public override string ToString()
{
return this.AreaName;
}
}
}
C# winform 选择项 省市连动的更多相关文章
- Js获取下拉框当前选择项的文本和值
现在有一个Id为AreaId的下拉框,要获取它当前选择项的文本和值有以下方法: <span class="red">* </span> 地 区: ...
- Chosen中选择项的更新
Chosen 选择项的动态修改/更新 如果你需要去动态更新select选择框里的选择项,你需要通知Chosen去响应这个变动,你需要在这个选项框是触发一个"liszt:updated&quo ...
- iOS开发——UI篇&下拉弹出列表选择项效果
下拉弹出列表选择项效果 右边菜单中的按键,点击弹出一个列表可选择,选择其中一个,响应相应的事件并把文字显示在右边的菜单上:弹出下拉效果使用LMDropdownView插件,可以用POD进行加载pod ...
- selenium 定位input输入框下的选择项
今天的问题与下图中的类似 这是一个input型输入框,当我点击或输入值时,输入框下方会显示选择项帮助快速输入,代码如下: <input class="v-input some" ...
- CentOS6.9-zabbix3.2启动失败原因及页面没有mysql选择项
环境内核信息: [root@zabbix- ~]# uname -a Linux lodboyedu- -.el6.x86_64 # SMP Tue Mar :: UTC x86_64 x86_64 ...
- WPF中使用后台代码来控制TreeView的选择项(SelectedItem)以及展开节点操作
首先为TreeView控件制作一个Style: <Style x:Key="LibraryTreeViewItemStyle" TargetType="{x:Typ ...
- springMVC 复选框带有选择项记忆功能的处理
前言:由于jsp管理页面经常会遇到复选框提交到JAVA后台,后台处理逻辑完成后又返回到jsp页面,此时需要记住jsp页面提交时复选框的选择状态,故编写此功能! 一.复选框的初始化 1.1.jsp页面 ...
- 使ListView控件中的选择项高亮显示
实现效果: 知识运用: ListView控件的SelectedItems属性 //获取在ListView控件中被选中数据项的集合 public ListView.SelectedListViewIte ...
- wpf ListBox删除选择项(支持多项)
搞了个ListBox删除选择项,开始老是不能把选择项删除干净,剩下几个.后来调试一下原来是ListBox在删除一个选择项之后立即更新,选择项也有变化.结果我想了个这样的方法来删除呵呵. Departm ...
随机推荐
- 破解NET的四大神器
原本这篇文章可以更早一星期写出来与大家分享,由于某方面的原因耽搁到现在,心里竟有那么一点好像对不住大家的感觉.这当然与神器有关,因为我发现利用这四大神器我似乎觉得几乎所有的NET程序破解都不在话下了 ...
- 剑指OFFER之反转链表(九度OJ1518)
题目描述: 输入一个链表,反转链表后,输出链表的所有元素.(hint : 请务必使用链表) 输入: 输入可能包含多个测试样例,输入以EOF结束.对于每个测试案例,输入的第一行为一个整数n(0<= ...
- 利用 SQL Monitor 查看语句运行状态步骤
利用 SQL Monitor 查看语句运行状态步骤 1.确定语句被 SQL Monitor 监控 SQL> SELECT * FROM GV$SQL_MONITOR WHERE sql_id=' ...
- Ext.tree.Panel Extjs 在表格中添加树结构,并实现节点移动功能
最近在用Extjs 做后台管理系统,真的非常好用.总结的东西分享一下. 先展示一下效果图 好了,开始吧! 首先说一下我的创建结构: 一.构造内容 这个函数中包括store的创建,treePanel的创 ...
- Codeforces Gym H. Hell on the Markets 贪心
Problem H. Hell on the MarketsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vj ...
- Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量
Problem J. TriatripTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/att ...
- visualsvn server 报错 can't read file "current":End of file out
需要删除文件:Repositories\ProjectName\db\revprops\X 需要修改文件:repository/db/txn_current.repository/db/current ...
- 合并js文件minify实例
将min目录放入项目中后,js中引入方式是: <script type="text/javascript" src="__PUBLIC__/min/?b=publi ...
- MySQL并发复制系列二:多线程复制
http://blog.itpub.net/28218939/viewspace-1975822/ 并发复制(Parallel Replication) 系列二: Enhanced Multi-th ...
- 打开Excel时提示"您尝试打开的文件**.xls的格式与文件扩展名指定的格式不一致"
问题描述: 系统安装了WPS时,Analyzer导出excel时候,会提示"您尝试打开的文件**.xls的格式与文件扩展名指定的格式不一致",这是Excel的安全问题, ...