组合框的值和文本的填充 [英] Populating Value and Text of ComboBox

查看:63
本文介绍了组合框的值和文本的填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单上有一个名为ddCoursescombobox.我试图用LINQ表达式加载它,但是当我运行程序时,combobox显示的是我用LINQ语句加载的值(COURSE_ID)和文本(COURSE_TITLE),而不仅仅是文本(I想要隐藏值(COURSE_ID).

I have a combobox called ddCourses on my form. I'm trying to load it with a LINQ expression, but when I run the program, the combobox is showing the value (COURSE_ID) and text (COURSE_TITLE) that I loaded with the LINQ statement, instead of just the text (I want the Value, COURSE_ID, hidden).

这是我的代码:

    private void LoadDropDowns()
    {
        var db = new DataClasses1DataContext();
        ddCourse.DataSource = (from c in db.COURSE_MASTERs
                               select new { c.COURSE_ID, c.COURSE_TITLE }).ToList();
    }

如何设置combobox每行的值和文本?

How can I set the Value and Text of each row of the combobox?

我看到的其他示例使用了DataTextFieldDataValueField属性,但是这些似乎对我来说不可用.

Other examples I've seen have used DataTextField and DataValueField properties, but these do not seem to be available to me.

我是否在我的using语句中缺少引用,该引用使我可以访问DataTextFieldDataValueField属性?

Am I missing a reference in my using statements that would allow me to have access to the DataTextField and DataValueField properties?

这是我的使用中"声明:

Here's my 'using' statements:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

DataTextField和DataValueField属性对我不可用.这是因为它们是System.Web.UI的一部分,并且这是我正在使用的Winform吗?

The DataTextField and DataValueField properties are not available to me. Is this because they are part of System.Web.UI and this is a winform I'm using?

推荐答案

您需要先设置DisplayMemberValueMember:

var db = new DataClasses1DataContext();
ddCourse.DisplayMember = "COURSE_TITLE";
ddCourse.ValueMember = "COURSE_ID";
ddCourse.DataSource = (from c in db.COURSE_MASTERs
                        select new { c.COURSE_ID, c.COURSE_TITLE }).ToList();

这篇关于组合框的值和文本的填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆