2012-09-10 154 views
0

我目前在C#窗口應用程序中工作。在那裏我使用DataGridView中的數據網格視圖組合框。單擊下拉框時,它會顯示名稱字段,如果我從下拉框中選擇名稱字段,它會在DataGridView ComboBox中顯示值成員值。爲什麼DataGridView ComboBox顯示ValueMember值?

爲什麼我沒有獲得組合框中的顯示成員值?

在databindindcomplete功能我定義的組合框的值:

((DataGridViewComboBoxColumn)dgvItem_1.Columns["Student"]).DataSource = objDBContext.Stu_student; 
((DataGridViewComboBoxColumn)dgvItem_1.Columns["Student"]).DisplayMember = "STUDENT_NAME"; 
((DataGridViewComboBoxColumn)dgvItem_1.Columns["Student"]).ValueMember = "STUDENT_ID"; 

如果我選擇在下拉顯示的下拉列表

名稱
下面的值在ComboBox值---- -
Raja
Ramesh
Rani

如果我在列表中選擇Raja,ComboBox將在ComboBox中顯示相應的STUDENT_ID。但我想在ComboBox中顯示學生名稱。

誰能告訴我爲什麼我在DataGridView ComboBox中獲取ValueMember值?

+0

哪種類型的'objDBContext.Stu_student'? –

回答

1

所以在我的簡單的例子,在那裏我有一個學生類,看起來像:

public class Student 
{ 
    public string Name { get; private set; } 
    public int StudentId{ get; private set; } 
    public Student(string name, int studentId) 
    { 
     Name = name; 
     StudentId= studentId; 
    } 

    private static readonly List<Student> students = new List<Student> 
    { 
     { new Student("Chuck", 1) }, 
     { new Student("Bob", 2) } 
    }; 

    public static List<Student> GetStudents() 
    { 
     return students ; 
    } 
} 

,我建立了我對ComboBox這樣的結合:

 DataGridViewComboBoxColumn comboBoxColumn = (DataGridViewComboBoxColumn)dataGridView1.Columns[0]; 
     comboBoxColumn .DataSource = Student.GetStudents(); 
     comboBoxColumn .DisplayMember = "Name"; 
     comboBoxColumn .ValueMember = "StudentId";   

我得到我的學生姓名在下拉列表中,當我選擇一個時,所選的姓名出現在單元格中。

+0

謝謝你的回答,現在我在ComboBox文本中獲得了顯示成員。 – Vinoth

+0

太好了。很高興有幫助。 – itsmatt