2011-09-07 78 views
1

我有問題,在WinForm的一個DataGridView內DataGridViewComboBoxColumn堅持用戶的選擇。一旦離開組合框,選擇就會消失。堅持用戶選擇在DataGridViewComboBoxColumn在winform

我會發現一些答案的問題,如將selectedIndex設置爲-1,但沒有奏效。請給我指出正確的方向。

在此先感謝。

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

     // Create DataTable. 
     DataColumn classIdColumn = new DataColumn("Class", typeof(string)); 
     _schoolTable = new DataTable("School"); 
     _schoolTable.Columns.AddRange(new[] { classIdColumn }); 
     DataRow row = _schoolTable.NewRow(); 
     row["Class"] = "yr 5"; 
     _schoolTable.Rows.Add(row); 

     // Bind DataGridView to DataTable, and add ComboBoxColumn. 
     dataGridView1.DataSource = _schoolTable; 
     DataGridViewComboBoxColumn listCol = new DataGridViewComboBoxColumn(); 
     listCol.DisplayIndex = 1; 
     listCol.DataSource = GetChoices(); 
     listCol.DisplayMember = "Category"; 
     listCol.ValueMember = "Number"; 
     listCol.DefaultCellStyle.NullValue = "None"; 
     dataGridView1.Columns.Add(listCol); 
    } 

    private DataTable _schoolTable; 

    private static List<IHuman> GetChoices() 
    { 
     return Choices; 
    } 

    private static readonly List<IHuman> Choices = new List<IHuman>(){ new Student(), new Teacher() }; 

    private interface IHuman 
    { 
     int Number { get; set; } 
     string Category { get; } 
    } 

    private class Student : IHuman 
    { 
     public int Number { get; set; } 
     public string Category { get { return "student"; } } 
    } 

    private class Teacher : IHuman 
    { 
     public int Number { get; set; } 
     public string Category { get { return "teacher"; } } 
    } 
} 
+0

什麼事件是您使用的datagridview的?如果你有一個cellformatting事件處理程序或類似的東西,就會發生這種情況。 –

+0

謝謝大衛。我沒有任何事件處理程序。只有當了「DataGridViewComboxBoxColumn」裏面的「的DataGridView」的「數據源」設置爲對象的列表出現此問題。並將此「DataGridViewComboxBoxColumn」的「DisplayMember」設置爲對象內的Property。如果「數據源」設置爲字符串「DisplayMember」的列表中未設置任何東西,當我離開組合框的選擇將保持均勻。 – Dominic

+0

你能提供一些代碼嗎?我從來沒有遇到過這樣的問題,所以我猜你正在做一些不尋常的事情或者不太正確。你可能重置了你綁定數值源對象的值成員的屬性? –

回答

0

此問題的最初原因是您沒有爲您的IHuman對象的Number屬性指定任何值。

如果你改變你的代碼行創建清單是這樣的:

private static readonly List<IHuman> Choices = new List<IHuman>() { new Student() {Number = 0}, new Teacher() {Number = 1} }; 

或把默認值到每個號碼屬性對象的工具IHuman如您對類別屬性,那麼你組合框應該正常工作。


此外,您可以使您的代碼更容易處理。

你可以做的第一件事是添加一列到您的數據表來支持你的組合框柱綁定,讓你只要看看數據表就知道選擇什麼。做到這一點的代碼如下:

DataColumn classIdColumn = new DataColumn("Class", typeof(string)); 
_schoolTable = new DataTable("School"); 

//Create a new column in the data table of type int 
DataColumn humanIdColumn = new DataColumn("HumanId", typeof(int)); 

_schoolTable.Columns.AddRange(new[] { classIdColumn }); 
_schoolTable.Columns.AddRange(new[] { humanIdColumn }); 

DataRow row = _schoolTable.NewRow(); 
row["Class"] = "yr 5"; 
_schoolTable.Rows.Add(row); 

// Bind DataGridView to DataTable, and add ComboBoxColumn. 
dataGridView1.DataSource = _schoolTable; 
DataGridViewComboBoxColumn listCol = new DataGridViewComboBoxColumn(); 
listCol.DisplayIndex = 1; 
listCol.DataSource = GetChoices(); 
listCol.DisplayMember = "Category"; 
listCol.ValueMember = "Number"; 
//Set the DataPropertyName on the comboboxcolumn to enable databinding 
listCol.DataPropertyName = "HumanId"; 
listCol.DefaultCellStyle.NullValue = "None"; 
dataGridView1.Columns.Add(listCol); 

//Hide the autogenerated column HumanId - we only have it for the databinding 
dataGridView1.Columns["HumanId"].Visible = false; 

這種變化之後,我會強烈建議使用自定義對象的名單,您的數據源,而當使用數據表 - 我一直認爲這是更加靈活。

+0

它的工作原理!非常感謝您的幫助。 – Dominic