2009-02-23 63 views
1

我有一個DataGridView有一個ComboBox列。我填充此列的有型的項目列表DataGridViewComboBoxColumn - 下拉列表中的項目類型

DataGridViewComboBoxColumn fieldsColumn = argumentsDataGridView.Columns["field"] as DataGridViewComboBoxColumn; 
      foreach (Field field in SessionData.Fields) 
       fieldsColumn.Items.Add(field); 
      fieldsColumn.DisplayMember = "Name"; 

再經過一些用戶的動作,我把一個價值在此列像這樣的小區:

private void AddArgument(string argumentName, Field field) 
     { 
      int index = argumentsDataGridView.Rows.Count; 
      argumentsDataGridView.Rows.Add(new DataGridViewRow()); 
      DataGridViewRow newRow = argumentsDataGridView.Rows[index]; 
      newRow.Cells["nameArg"].Value = argumentName; 
      -> newRow.Cells["field"].Value = field; 
     } 

如果我現在訪問單元格的值,它是一個類型字段。如果我從組合中選擇不同的項目,那麼單元格的值將變成一個字符串。我該如何處理?我需要類型的項目

+0

你甚至如何得到這個工作?當我運行一個簡單的測試時,我得到一個System.ArgumentException? – BFree 2009-02-23 14:32:32

+0

你在哪裏得到這個異常? – agnieszka 2009-02-23 14:43:52

回答

3

的解決方案是在課堂上創造財產

public Field Self 
     { 
      get { return this; } 
     } 

,並設置作爲組合框列的ValueMember。
我認爲沒有指定ValueMember 是一個默認的返回值,我錯了。