加载中…
个人资料
小宇
小宇
  • 博客等级:
  • 博客积分:0
  • 博客访问:101,723
  • 关注人气:20
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

C# Combobox控件的使用

(2013-07-26 19:07:07)
标签:

it

分类: 软件技术

C# Combobox控件的使用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Combox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(@"Data Source=localhost;Initial Catalog=DB_Test;User ID=sa;PassWord=sa;Pooling=False"))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from promary";
                    using (SqlDataReader datareader = cmd.ExecuteReader())
                    {
                        while (datareader.Read())
                        {
                            provinceItem item = new provinceItem();
                            item.Id = datareader.GetInt32(datareader.GetOrdinal("proID"));
                            item.Name = datareader.GetString(datareader.GetOrdinal("proName"));
                            cmbPro.Items.Add(item);
                        }
                    }
                }
            }
        }

 
        private void cmbPro_SelectedIndexChanged(object sender, EventArgs e)
        {
            provinceItem item = (provinceItem)cmbPro.SelectedItem;//将selecteditem本身为object类型,转换为provinceitem类型
            int proId = item.Id;
            using (SqlConnection conn = new SqlConnection(@"Data Source=localhost;Initial Catalog=DB_Test;User ID=sa;PassWord=sa;Pooling=False"))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from city where proID=@proID";
                    cmd.Parameters.Add(new SqlParameter("proID",proId));

                    using (SqlDataReader datareader = cmd.ExecuteReader())
                    {
                        while (datareader.Read())
                        {
                            string cityName = datareader.GetString(datareader.GetOrdinal("cityName"));
                            cmbcity.Items.Add(cityName);

                        }
                    }
                }
            }
        }

    }

    class provinceItem//定义类和属性,并将combobox控件的displaymember的属性值设为Name
    {
        private string name;
        public string Name {get{ return name; } set { name = value; }}
        private int id;
        public int Id { get{ return id; } set { id = value; }}
    }
}

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有