zoukankan      html  css  js  c++  java
  • combox 绑定数据库

    combox 有两个属性值:ValueMember 、DisplayMember、前者对应于value、后者对应text 

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 using System.Windows.Forms;
    10 using System.Data.SqlClient;
    11 using System.Data.Sql;
    12 
    13 namespace CheckedlistBox
    14 {
    15     public partial class Form1 : Form
    16     {
    17         public Form1()
    18         {
    19             InitializeComponent();
    20             //string strconnection = "server=172.168.1.10;database=wmts;uid=wengtai;pwd=wingtech2013";
    21             string strconnection = "server=.;database=wmts;uid=sa;pwd=test";
    22             string strcommand = "select distinct Model from FpOrderNumber";
    23 
    24             using (SqlConnection connection = new SqlConnection(strconnection))
    25             {
    26                 connection.Open();
    27                 SqlCommand command = new SqlCommand(strcommand, connection);
    28                 SqlDataAdapter adp = new SqlDataAdapter(command);
    29                 DataTable dt = new DataTable();
    30                 adp.Fill(dt);
    31                 comboBox1.DataSource = dt;
    32                 //使用combox.text 获取当前的值时 ValueMember,DisplayMember 二者都可以使用
    33                 //使用comboBox1.SelectedValue获取当前的值时,必须使用ValueMember
    34 
    35                 //comboBox1.ValueMember = "Model";
    36                 comboBox1.DisplayMember = "Model";
    37 
    38             }
    39             //button1.Click += new EventHandler(Button1_Click);
    40             comboBox1.SelectedValueChanged += new EventHandler(Button1_Click);
    41             
    42         }
    43         
    44         private void Button1_Click(object sender, EventArgs e)
    45         {
    46             string strmessage = string.Empty;
    47 
    48             //strmessage += "Combox选中:" + comboBox1.SelectedValue + "
    ";
    49             strmessage += "Combox选中:" + comboBox1.Text + "
    ";
    50 
    51             //strmessage += "Combox选中:" + comboBox1.SelectedText + "
    ";
    52             //strmessage += "Combox选中:" + comboBox1.SelectedItem.ToString() + "
    ";
    53 
    54             richTextBox1.AppendText(strmessage);
    55             richTextBox1.AppendText("
    ");
    56         }
    57     }
    58 
    59    
    60 }

  • 相关阅读:
    每日日报
    剑指 Offer 18. 删除链表的节点(LeetCode)
    java的访问权限
    java从键盘输入
    剑指 Offer 22. 链表中倒数第k个节点(快慢指针)(LeetCode)
    面试题 02.03. 删除中间节点(LeetCode)
    21. 合并两个有序链表(Leetcode)
    计算总线数据传输率
    时钟周期、总线周期(机器周期)区别
    书单(个人)
  • 原文地址:https://www.cnblogs.com/wenjie0904/p/7643251.html
Copyright © 2011-2022 走看看