2016-07-29 133 views
0

grivviewDevexpressCheckedListBoxItem,你可以在這裏看到:獲取CheckedListBoxItem的DevExpress在GridView控件的值c#

enter image description here

我初始化數據源中page_load你可以看到:

List<User> confirms = _userRepository.Get().ToList(); 
      ConfirmList.DataSource = confirms; 
      ConfirmList.DisplayMember = "FullName"; 
      ConfirmList.ValueMember = "Id"; 

在保存按鈕中,我需要獲取用戶選定的值(多個選項),但它返回null爲什麼?

private void btnSave_ItemClick_1(object sender, ItemClickEventArgs e) 
{ 
    gridView.CloseEditor(); 
    Convert.ToDateTime(gridView.GetRowCellValue(rowHandle, "ReturnDateTime")); 
    CheckedListBoxItem confirms =(CheckedListBoxItem)(gridView.GetRowCellValue(rowHandle, "Confirm")); 
} 
+0

你怎麼在「確認」欄中存儲?正如我可以從列表的數據源中看到的那樣。它應該具有與User的Id屬性類似的數據類型。 –

+0

@NiranjanKala這是什麼意思?我在問題 –

回答

0

正如我可以懷疑你的代碼,你正在鑄造gridView.GetRowCellValue(rowHandle, "Confirm")返回值爲無效類型。使用as運算符更改以下代碼行。

CheckedListBoxItem confirms =(CheckedListBoxItem)(gridView.GetRowCellValue(rowHandle, "Confirm")); 

CheckedListBoxItem confirms = gridView.GetRowCellValue(rowHandle, "Confirm") as CheckedListBoxItem; 
if(confirms != null){} 

這樣做,你就會知道得到的結果是什麼調試後。

,我可以看到編輯器上安裝有列Confirm那麼你會從gridView.GetRowCellValue()得到的結果是User類不是CheckedListBoxItemId屬性值。

當您調用gridView.CloseEditor();時,編輯器將不存在以獲取CheckedListBoxItem。您可以訪問ColumnView.ShownEditor Event上的編輯器。請參見下面的代碼片段:

private void MainForm_Load(object sender, EventArgs e) { 
    this.PhonesSource.DataSource = DataContext.GetPhones(); 
    this.CountriesSource.DataSource = DataContext.GetCountries(); 
    this.CitiesSource.DataSource = DataContext.GetAllCities(); 
} 

private void GridView_ShownEditor(object sender, EventArgs e) { 
    ColumnView view = (ColumnView)sender; 
    if (view.FocusedColumn.FieldName == "CityCode") { 
     LookUpEdit editor = (LookUpEdit)view.ActiveEditor; 
     string countryCode = Convert.ToString(view.GetFocusedRowCellValue("CountryCode")); 
     editor.Properties.DataSource = DataContext.GetCitiesByCountryCode(countryCode); 
    } 
} 

// In certain scenarios you may want to clear the secondary editor's value 
// You can use the RepositoryItem.EditValueChanged event for this purpose 
private void CountryEditor_EditValueChanged(object sender, EventArgs e) { 
    this.GridView.PostEditor(); 
    this.GridView.SetFocusedRowCellValue("CityCode", null); 
} 


    private void MainForm_Load(object sender, EventArgs e) { 
     this.PhonesSource.DataSource = DataContext.GetPhones(); 
     this.CountriesSource.DataSource = DataContext.GetCountries(); 
     this.CitiesSource.DataSource = DataContext.GetAllCities(); 
    } 

    private void GridView_ShownEditor(object sender, EventArgs e) { 
     ColumnView view = (ColumnView)sender; 
     if (view.FocusedColumn.FieldName == "CityCode") { 
      LookUpEdit editor = (LookUpEdit)view.ActiveEditor; 
      string countryCode = Convert.ToString(view.GetFocusedRowCellValue("CountryCode")); 
      editor.Properties.DataSource = DataContext.GetCitiesByCountryCode(countryCode); 
     } 
    } 

希望這有助於..

+0

感謝您的代碼,但它不再工作,它返回null –

+0

你的解決方案是類似於我的建議,你將只會在選擇後從網格單元獲得密鑰。您可以使用這些鍵從數據源中獲取實際記錄。如果您的列表允許多選,那麼您的問題與您的問題文本無關。沒有人可以判斷你想從列表中獲取多個值。解決這個問題真是太好了 –

-1

我認爲鑄造類型是問題。

+0

中解釋了我應該使用哪個投射? –