Get the value of CheckedListBoxItem devexpress in gridview c#

妖精的绣舞 提交于 2019-12-25 09:08:15

问题


I put a CheckedListBoxItem in grivviewDevexpress as you can see here :

I initialize the datasource in page_load as you can see :

 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 ?

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

回答1:


As i can suspect about your code that you are casting gridView.GetRowCellValue(rowHandle, "Confirm") returned value to invalid type. Change the below your below line of code by using the as operator.

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.

When you call the gridView.CloseEditor(); then editor will not exist to get the CheckedListBoxItem. You can access the editor on the ColumnView.ShownEditor Event. 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..




回答2:


I think the casting type is the problem.



来源:https://stackoverflow.com/questions/38655058/get-the-value-of-checkedlistboxitem-devexpress-in-gridview-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!