在gridview c#中获取CheckedListBoxItem devexpress的值

时间:2016-07-29 09:19:23

标签: c# gridview devexpress

我在CheckedListBoxItem中添加grivviewDevexpress,如您所见:

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

2 个答案:

答案 0 :(得分: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() Id类的User属性值而非CheckedListBoxItem的结果{1}}。

当您调用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);
        }
    }

希望这有帮助..

答案 1 :(得分:-1)

我认为铸造类型是问题所在。