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 ...
随机推荐
- 使用Windbg和SoS扩展调试分析.NET程序
在博客堂的不是我舍不得 - High CPU in GC(都是+=惹的祸,为啥不用StringBuilder呢?). 不是我舍不得 - .NET里面的Out Of Memory 看到很多人在问如何分析 ...
- iOS开发-为程序添加应用设置
一.设置捆绑包 设置捆绑包是应用自带的一组文件,用于告诉设置该应用期望得到用户的哪些偏好设置. 新建设置捆绑包:Command+N,在iOS部分中的Resource,选择Settings Bundle ...
- WinDbug之DUMP蓝屏分析
Microsoft (R) Windows Debugger Version 6.2.8400.0 X86Copyright (c) Microsoft Corporation. All rights ...
- VB连接Mysql数据库
当然机器装有mysql数据库 然后下载安装Mysql,odbc驱动 须要加入ado'菜单"project"->"引用" 找 Microsoft Activ ...
- 基础数据结构 之 队列(python实现)
队也是编程开发中常见的一种数据结构.栈和队可用来模拟函数的递归过程.队的特点为先入先出,主要操作包括入队和出队.入队时需判断队是否已满,出队时需判断队是否为空.下面给出一个队的python实现的例子: ...
- WCF 新手教程二
基本知识: [ServiceContract] Attribute 能够有以下Property 的: CallbackContract 设置callback的类型:Duplicate指Service ...
- HDU 4121 Xiangqi 模拟题
Xiangqi Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4121 ...
- Codeforces Gym 100637B B. Lunch 找规律
B. Lunch Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/B Des ...
- 【zabbix系列】报警系统的设置和排除
关于邮件报警,有非常多方案,这里选择的是稳定性较好.使用较多的msmtp+mutt方案. 该方案有一个非常好的地方在于不用自己来搭建独立的mailserver,能够使用第三方mail.这样的方法不仅能 ...
- linux下xargs命令用法详解
原文:http://blog.chinaunix.net/uid-128922-id-289992.html xargs在linux中是个很有用的命令,它经常和其他命令组合起来使用,非常的灵活. xa ...