博客
关于我
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/

    你可能感兴趣的文章
    python 保留精度及增加去除数字的千位分隔符(金额化数字)
    查看>>
    python 倒计时 9,8,7,。。。。。。0
    查看>>
    Python 入门开发学习笔记之数据的增删改查
    查看>>
    Python 入门教程(2)搭建环境 2.4、VSCode配置Node.js运行环境
    查看>>
    Python 八大排序算法合集
    查看>>
    python 关于epoll的学习
    查看>>
    Python 内存管理
    查看>>
    Python 内嵌函数:它们有什么用处?
    查看>>
    Python 内置 sum 函数 vs. for 循环性能
    查看>>
    python 内置slice的用法
    查看>>
    Python 内置时间模块
    查看>>
    python 内部如何实现命名元组?
    查看>>
    Python 写Android App性能:入门到高级
    查看>>
    python 写入json数据到数据库
    查看>>
    Python 出现 TypeError: ‘encoding‘ is an invalid keyword argument for this function 解决方法
    查看>>
    python 出现 TypeError: ‘list‘ object is not callable 的解决方法
    查看>>
    python 函数1
    查看>>
    python 函数学习
    查看>>
    Python 函数随笔 map()函数,lambda自定义函数
    查看>>
    Python 分析天气,告诉你中秋应该去哪里
    查看>>