zoukankan      html  css  js  c++  java
  • Combobox.Items中添加项Items

    Solution 1:

    不想用绑定的话,自已写一个实现Value和Text属性的类,然后重写ToString()方法,并返回和Text属性一样的值,然后把这个类的实际Add到ComboBox.Items中,取值时再转换一下类型.  
      当你往ComboxBox的Item中扔一个任意object时,显示出的内容就是这个object的ToString()方法.所以必须重写ToString()方法,来显示你要显示的内容.

    Solution 2:

    不妨定义一个两列的DataTable为comboBox为数据源,将其与ComboBox绑定,添加事件时只要往这个DataTable添加行就可以了。  
      DataTable   dt;  
      private   void   Form1_Load(object   sender,   System.EventArgs   e)  
      {  
      dt   =   new   DataTable("dt");  
      dt.Columns.Add("dtText");  
      dt.Columns.Add("dtValue");  
      comboBox1.DataSource   =   dt;  
      comboBox1.DisplayMember   =   "dtText";  
      comboBox1.ValueMember   =   "dtValue";  
      }  
      private   void   button2_Click(object   sender,   System.EventArgs   e)  
      {  
      dt.Rows.Add(new   object[]{"Text",value});  
      }  

    Solution 3:

    还有这样一个类,可以专门用来向列表控件添加列表项,这个类就是 DictionaryEntry ,关于这个类的使用方法及其相关介绍,你可以在你的MSDN中找到,如果您没有安装MSDN,请点击这里

            Do While Reader.Read
                        ComboBox1.Items.Add(New DictionaryEntry(Reader.Item("显示字段"), Reader.Item("值字段")))
            Loop
            ComboBox1.DisplayMember = "Key"
            ComboBox1.ValueMember = "Value"
    在使用DictionaryEntry对象向列表控件添加数据后, 如果要得到被选定项的“显示的文本”或“项的值”,那么,请使用如下的写法,

    Dim strValue As String = CType(ComboBox1.SelectedItem, DictionaryEntry).Value
    Dim strKey As String = CType(ComboBox1.SelectedItem, DictionaryEntry).Key

    出处:http://blog.csdn.net/xueweiheng/archive/2007/06/04/1637735.aspx

  • 相关阅读:
    curl 文件上传
    地区选择三级联动
    水平居中
    z-index
    css垂直居中
    Enrolment API
    阿里云OSS存储
    java中的vo、dto 、dao
    Spring mvc 中使用 kaptcha 验证码
    将下载的本地的jar手动添加到maven仓库
  • 原文地址:https://www.cnblogs.com/emanlee/p/1527359.html
Copyright © 2011-2022 走看看