组合框的值和文本的填充 [英] Populating Value and Text of ComboBox
问题描述
我的表单上有一个名为ddCourses
的combobox
.我试图用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
?
我看到的其他示例使用了DataTextField
和DataValueField
属性,但是这些似乎对我来说不可用.
Other examples I've seen have used DataTextField
and DataValueField
properties, but these do not seem to be available to me.
我是否在我的using
语句中缺少引用,该引用使我可以访问DataTextField
和DataValueField
属性?
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?
推荐答案
您需要先设置DisplayMember
和ValueMember
:
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屋!