博客
关于我
C#开发之——组合框控件数据绑定(15.11)
阅读量:105 次
发布时间:2019-02-26

本文共 2276 字,大约阅读时间需要 7 分钟。

????????

?Windows????????????????DataSource????????DataSet?DataTable??????????????????????????????????

???????????????TextBox?????Label??????ListBox??????ComboBox????????DataGridView?????????????????????????????????????????????????????????????????

??????????????????????????


???????????

2.1 ???????

??????ComboBox??Windows?????????????????????????Windows????????????????????????????

??????????

  • ?????????????????????????????????ComboBox??????
  • ?????????????????????????????????????????
  • ?????????????????????????????????????DataSource???
  • ???????????????
    • ???????????????????????????Text?????
    • ?????????????????????????Value?????
    • ?????????????????????

  • 2.2 ???????

    ??????????????????????????????DataSource?DisplayMember?ValueMember???????????

    ???????

    ComboBox comboBox1 = new ComboBox();  SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM major", conn);  DataSet ds = new DataSet();  sda.Fill(ds);  comboBox1.DataSource = ds.Tables[0];  comboBox1.DisplayMember = "name";  comboBox1.ValueMember = "id";

    ??????

    3.1 ?????????

    3.1.1 ??????

    ????T-SQL??????????

    CREATE TABLE major (      id INT PRIMARY KEY IDENTITY(1,1),      name VARCHAR(20) UNIQUE  );

    3.1.2 ??????????

  • ??????????????????????
  • ???ComboBox?????????????
    • ???????????????????????????????????????
    • ????????????????????
  • ????????????name?????????????id??????????????????

  • 3.2 ?????????????

    3.2.1 ????

    private void ComboBoxForm_Load(object sender, EventArgs e)  {      SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=test;User ID=sa;Password=root");      try      {          conn.Open();          SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM major", conn);          DataSet ds = new DataSet();          sda.Fill(ds);          comboBox1.DataSource = ds.Tables[0];          comboBox1.DisplayMember = "name";          comboBox1.ValueMember = "id";      }      catch (Exception ex)      {          MessageBox.Show("?????" + ex.Message);      }      finally      {          if (conn != null)          {              conn.Close();          }      }  }

    3.2.2 ????????

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)  {      if (comboBox1.Tag != null)      {          MessageBox.Show("????????" + comboBox1.Text);      }  }

    3.3 ??

    ???????????????????????????????????????????????

    转载地址:http://atpz.baihongyu.com/

    你可能感兴趣的文章
    PostgreSQL学习总结(10)—— PostgreSQL 数据库体系架构
    查看>>
    PostgreSQL学习总结(11)—— PostgreSQL 常用的高可用集群方案
    查看>>
    Qt开发——多线程网络时间客户端
    查看>>
    PostgreSQL学习总结(13)—— PostgreSQL 15.8 如何成就数据库性能王者?
    查看>>
    PostgreSQL学习总结(13)—— PostgreSQL 目录结构与配置文件 postgresql.conf 详解
    查看>>
    PostgreSQL学习总结(1)—— PostgreSQL 入门简介与安装
    查看>>
    PostgreSQL学习总结(2)—— PostgreSQL 语法
    查看>>
    PostgreSQL学习总结(3)—— PostgreSQL 数据类型
    查看>>
    Qt开发——圆面积计算器
    查看>>
    PostgreSQL学习总结(5)—— PostgreSQL table 创建与删除
    查看>>
    PostgreSQL学习总结(6)—— PostgreSQL 模式(SCHEMA)详解
    查看>>
    PostgreSQL学习总结(7)—— PostgreSQL 语句 INSERT INTO、SELECT、UPDATE、DELETE 等学习
    查看>>
    PostgreSQL学习总结(8)—— PostgreSQL 基于数据库和基于模式(schema)的多租户分析
    查看>>
    PostgreSQL学习总结(9)—— PostgreSQL 运算符与表达式
    查看>>
    PostGreSql学习笔记001---PostgreSQL10.4安装(Windows)_支持PostGreGis_PostJDBC
    查看>>
    PostGreSql学习笔记002---Navicat Premium中管理PostGreSql 错误:字段rolcatupdate 不存在
    查看>>
    PostgreSQL学习笔记:PostgreSQL vs MySQL
    查看>>
    PostgreSQL实现shape数据转geojson数据(地图工具篇.18)
    查看>>
    PostgreSQL导入shape数据(地图工具篇.10)
    查看>>
    PostGreSql工作笔记003---在Navicat中创建数据库时报错rolcatupdate不存在_具体原因看其他博文_这里使用pgAdmin4创建管理postgre
    查看>>