簡體   English   中英

在gridview中獲取CheckedListBoxItem devexpress的值C#

[英]Get the value of CheckedListBoxItem devexpress in gridview c#

我將CheckedListBoxItem放在grivviewDevexpress ,您可以在這里看到:

在此處輸入圖片說明

如您所見,我在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"));
 }

正如我可以懷疑的那樣,您正在將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類的Id屬性值,而不是CheckedListBoxItem

調用gridView.CloseEditor(); 那么編輯器將不存在,無法獲取CheckedListBoxItem。 您可以在ColumnView.ShownEditor事件上訪問編輯器。 請參閱以下代碼段:

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);
        }
    }

希望有幫助。

我認為類型轉換是問題所在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM