惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
B
Blog RSS Feed
美团技术团队
博客园 - 【当耐特】
C
Check Point Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
aimingoo的专栏
aimingoo的专栏
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 司徒正美
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
Google DeepMind News
Google DeepMind News
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
N
Netflix TechBlog - Medium
Vercel News
Vercel News
P
Proofpoint News Feed
IT之家
IT之家
I
InfoQ
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - niuyy

博文阅读密码验证 - 博客园 博文阅读密码验证 - 博客园 博文阅读密码验证 - 博客园 .net 中用到excel中,检索COM类工厂中的CLSID为{000 24500-0000-0000-C000-000000000046}的组件时失败 客户端获取传递的参数 SQL语句中,插入单引号、添加、删除字段 JS给RadioButtonList赋值 在SQL2000中不显示系统表及系统文件 Embed标签详解 可以拖动,关闭的DIV组合 获取计算机用户名 浏览器字体变小 学习小技巧 ComBox 绑定数据库 C# 读取文本文件 字符编码 在.net中应用事务 数据库索引应用(ms-sql) 开发人员组要注意的网站 C# 在.cs文件中使用正则表达式
ComboBox与其他控件的联动
niuyy · 2007-12-19 · via 博客园 - niuyy

当选择ComboBox的值时其他控件也跟着变化。
窗体控件:comboBox1comboBox2,listBox1textBox1
数据库表字段:ID Name ParentID


 
private void Form1_Load(object sender, EventArgs e)
        
{  DataBindComboBox();
   
        }

  
public void DataBindComboBox()
        
{
            BindingSource bindingSource 
= new BindingSource();
            bindingSource.DataSource 
= myDB.DatabindComboBox(string.Empty).Tables[0];
            comboBox1.DataSource 
= bindingSource;          
            comboBox1.DisplayMember 
= "Name";           
            
        }

事件:

/// <summary>
        
/// 根据选择的省份获取省份的市
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        
{            
            
string Condition = string.Format(" where ParentID in(select ID from DeptTree where Name='{0}')", comboBox1.Text);
             DataTable  dataTable
=myDB.DatabindComboBox(Condition).Tables[0];
             
if (dataTable.Rows.Count > 0)
             
{
                 comboBox2.DataSource 
= dataTable;
                 comboBox2.DisplayMember 
= "Name";
                 listBox1.DataSource 
= dataTable;
                 listBox1.DisplayMember 
= "Name";
                
             }

             
else
             
{
                 comboBox2.DataSource 
= null;
                 listBox1.DataSource 
= null;
             }


           
        }


        
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        
{
            textBox1.Text 
= listBox1.Text.ToString();
        }

   /// <summary>
        
/// 把地名显示到控件中
        
/// </summary>
        
/// <returns></returns>

        public DataSet DatabindComboBox(string Condition)
        
{
            
string sqlstr = "select Name from DeptTree" + Condition;
            
return RunSQL_DataTable(sqlstr);
        }