星期零

技术改变生活,分享让我们快乐!
随笔 - 159, 文章 - 0, 评论 - 234, 阅读 - 44万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

winform的ComboBox中只能赋值text,显示和值是一样的,很多时候不能满足根本需要,熟悉B/S开发的coder最常用的就是text和value分开的,而且web下DropDownList本来就是分为text和value。ComboBox要实现同样功能,使item有多个值,只能用重写一个类来实现了。

 

重写类如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
 
namespace sm
{
    class cListItem
    {
        private string id = string.Empty;
        public string ID
        {
            get { return id; }
            set { id = value; }
        }
 
        private string name = string.Empty;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
 
        public cListItem(string name, string id)
        {
            this.id = id;
            this.name = name;
        }
 
        public override string ToString()
        {
            return this.name;
        }
 
    }
}

  绑定数据时:

1
bubufxComboBox.Items.Add(new cListItem(drv["bubufx_text"].ToString(), drv["ID"].ToString()));

  取值时:

1
string bubufxComboBox_str = ((cListItem)bubufxComboBox.SelectedItem).ID;

  bubufx分享,禁止转载。原文:【winform中ComboBox实现text和value,使显示和值分开,重写text和value属性】

努力加载评论中...
点击右上角即可分享
微信分享提示