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

    你可能感兴趣的文章
    nsis 安装脚本示例(转)
    查看>>
    NSJSON的用法(oc系统自带的解析方法)
    查看>>
    nslookup 的基本知识与命令详解
    查看>>
    NSNumber与NSInteger的区别 -bei
    查看>>
    NSOperation基本操作
    查看>>
    NSRange 范围
    查看>>
    NSSet集合 无序的 不能重复的
    查看>>
    NSURLSession下载和断点续传
    查看>>
    NSUserdefault读书笔记
    查看>>
    NS图绘制工具推荐
    查看>>
    NT AUTHORITY\NETWORK SERVICE 权限问题
    查看>>
    NT symbols are incorrect, please fix symbols
    查看>>
    ntelliJ IDEA 报错:找不到包或者找不到符号
    查看>>
    NTFS文件权限管理实战
    查看>>
    ntko web firefox跨浏览器插件_深度比较:2019年6个最好的跨浏览器测试工具
    查看>>
    ntko文件存取错误_苹果推送 macOS 10.15.4:iCloud 云盘文件夹共享终于来了
    查看>>
    ntp server 用法小结
    查看>>
    ntpdate 通过外网同步时间
    查看>>
    ntpdate同步配置文件调整详解
    查看>>
    NTPD使用/etc/ntp.conf配置时钟同步详解
    查看>>