C#中 combobox 显示数值和实际值怎么绑定

比如显示中国对应值CN,即显示给用户看的是"中国",实际值是CN求代码... 比如显示 中国对应值CN,即显示给用户看的是 "中国 ",实际值是CN 求代码 展开
 我来答
大别山的蜗牛
高粉答主

2016-01-23 · 一只互联网搬运的蜗牛
大别山的蜗牛
采纳数:6928 获赞数:12024

向TA提问 私信TA
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Collections;

namespace ClientDemo
{
    public partial class Setting : Office2007Form
    {
        private int start;
        private int process;
        private int end;
        private int faceQuality;
        private int faceConfidence;
        private int processIncrement;
        private int endIncrement;
        private int interval;
        private int resolution;

        public Setting()
        {
            InitializeComponent();
            /// <summary>
            /// 0=CIF, 1=QCIF, 2=D1 3=UXGA(1600x1200), 4=SVGA(800x600), 5=HD720p(1280x720),6=VGA
            /// IPCAM专用{3=UXGA(1600x1200), 4=SVGA(800x600), 5=HD720p(1280x720),6=VGA(640x480) , 7=XVGA, 8=HD900p }
            /// </summary>
            ArrayList al = new ArrayList();
            al.Add(new TextAndValue("CIF(352*288)", "0"));
            al.Add(new TextAndValue("QCIF(176*144)", "1"));
            al.Add(new TextAndValue("4CIF(704*576)", "2"));
            al.Add(new TextAndValue("UXGA(1600*1200)", "3"));
            al.Add(new TextAndValue("SVGA(800*600)", "4"));
            al.Add(new TextAndValue("HD720P(1280*720)", "5"));
            al.Add(new TextAndValue("VGA(640*480)", "6"));
            al.Add(new TextAndValue("XVGA(1280*960)", "7"));
            al.Add(new TextAndValue("HD900P(1600*912)", "8"));

            cboResolution.DataSource = al;
            cboResolution.DisplayMember = "DisplayText";
            cboResolution.ValueMember = "RealValue";
        }

        private void Setting_Load(object sender, EventArgs e)
        {
            DBColection.getConfigList();
            foreach (DBColection.TraceConfig tc in DBColection.configList)
            {
                if (tc.ParamType == "Start") txtMinStartTraceStart.Text = tc.Start.ToString();
                else if (tc.ParamType == "Process")
                {
                    txtMinStartTraceProcess.Text = tc.Start.ToString();
                    nudProcessIncrement.Value = (decimal)tc.Step;
                }
                else if (tc.ParamType == "End")
                {
                    txtMinStartTraceEnd.Text = tc.Start.ToString();
                    nudEndIncrement.Value = (decimal)tc.Step;
                }
                else if (tc.ParamType == "FaceQuality") txtFaceQuality.Text = tc.Start.ToString();
                else if (tc.ParamType == "FaceConfidence") txtFaceConfidence.Text = tc.Start.ToString();
                else if (tc.ParamType == "Interval") txtInterval.Text = tc.Start.ToString();
                else if (tc.ParamType == "Resolution")
                    cboResolution.SelectedValue = tc.Start.ToString();
            }
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnok_Click(object sender, EventArgs e)
        {
            start = Convert.ToInt32(txtMinStartTraceStart.Text.Trim().ToString());
            process = Convert.ToInt32(txtMinStartTraceProcess.Text.Trim().ToString());
            end = Convert.ToInt32(txtMinStartTraceEnd.Text.Trim().ToString());
            faceQuality = Convert.ToInt32(txtFaceQuality.Text.Trim().ToString());
            faceConfidence = Convert.ToInt32(txtFaceConfidence.Text.Trim().ToString());
            processIncrement = Convert.ToInt32(nudProcessIncrement.Value.ToString());
            endIncrement = Convert.ToInt32(nudEndIncrement.Value.ToString());
            interval = Convert.ToInt32(txtInterval.Text.Trim().ToString());
            resolution = Convert.ToInt16(cboResolution.SelectedValue.ToString());
            DBColection.updateConfigList(start, process, end, faceQuality, faceConfidence, processIncrement, endIncrement, interval, resolution);
        }

        private void cboResolution_SelectedIndexChanged(object sender, EventArgs e)
        {
            resolution = Convert.ToInt16(cboResolution.SelectedValue.ToString());
        }

    }
    class TextAndValue
    {
        private string _RealValue = "";
        private string _DisplayText = "";

        public string DisplayText
        {
            get
            {
                return _DisplayText;
            }
        }

        public string RealValue
        {
            get
            {
                return _RealValue;
            }
        }

        public TextAndValue(string ShowText, string RealVal)
        {
            _DisplayText = ShowText;
            _RealValue = RealVal;
        }

        public override string ToString()
        {
            return _RealValue.ToString();
        }

    }
}
尉迟丶九方
推荐于2017-09-13 · TA获得超过148个赞
知道小有建树答主
回答量:158
采纳率:0%
帮助的人:184万
展开全部
使用绑定数据集,如你要绑定的数据集为dv对象(DataView),其中包含ID和Name两列数据,分别为实际值和显示文本。combobox的对象名为cmb,实现代码如下:
cmb.DataSource=dv;
cmb.DisplayMember="Name";
cmb.ValueMember="ID";
cmb.DataBind();
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
a1012144015
2015-11-28 · TA获得超过6415个赞
知道大有可为答主
回答量:9038
采纳率:40%
帮助的人:1306万
展开全部

您好,很高兴为您解答!

思路如1,代码如2所示:

  1. 使用绑定数据集,如你要绑定的数据集为dv对象(DataView),其中包含ID和Name两列数据,分别为实际值和显示文本。

  2. combobox的对象名为cmb,实现代码如下:
    cmb.DataSource=dv;
    cmb.DisplayMember="Name";
    cmb.ValueMember="ID";
    cmb.DataBind();

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
灰姑娘的霸气
2015-12-12 · TA获得超过1.3万个赞
知道大有可为答主
回答量:3145
采纳率:72%
帮助的人:230万
展开全部
this.comboBox1.DisplayMember = "显示出来那个字段";
this.comboBox1.ValueMember = "这个是对应那个value值,通常也就是id值";
this.comboBox1.DataSource=数据源;
接下来绑定数据
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
leiyangbdwk
2012-02-21 · TA获得超过3295个赞
知道大有可为答主
回答量:4975
采纳率:12%
帮助的人:4331万
展开全部
你就写个结构体
struct Combo
{
string Show;
string Hide;
}
然后定义个此类型的数组,把你所有的配对都存进去。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(5)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式