简体   繁体   English

在gridview中获取CheckedListBoxItem devexpress的值C#

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

I put a CheckedListBoxItem in grivviewDevexpress as you can see here : 我将CheckedListBoxItem放在grivviewDevexpress ,您可以在这里看到:

在此处输入图片说明

I initialize the datasource in page_load as you can see : 如您所见,我在page_load初始化了数据源:

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

In save button I need to get the selected values(more than one selections) by user but it returns null why ? 在保存按钮中,我需要按用户获取选定的值(多个选择),但返回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"));
 }

As i can suspect about your code that you are casting gridView.GetRowCellValue(rowHandle, "Confirm") returned value to invalid type. 正如我可以怀疑的那样,您正在将gridView.GetRowCellValue(rowHandle, "Confirm")返回的值转换为无效类型。 Change the below your below line of code by using the as operator. 使用as运算符更改下面的代码行下方。

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

to

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

After doing that you will know get what is the result to debug. 这样做之后,您将知道得到什么调试结果。

As i can see that editor is attached with the column Confirm then you will get the result from gridView.GetRowCellValue() is Id property value of the User class not the CheckedListBoxItem . 正如我看到的那样,编辑器附带有“ Confirm ”列,那么您将从gridView.GetRowCellValue()获得结果,它是User类的Id属性值,而不是CheckedListBoxItem

When you call the gridView.CloseEditor(); 调用gridView.CloseEditor(); then editor will not exist to get the CheckedListBoxItem. 那么编辑器将不存在,无法获取CheckedListBoxItem。 You can access the editor on the ColumnView.ShownEditor Event . 您可以在ColumnView.ShownEditor事件上访问编辑器。 See the below code snippet: 请参阅以下代码段:

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

Hope this help.. 希望有帮助。

我认为类型转换是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM