问题
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