我在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"));
}
答案 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)
我认为铸造类型是问题所在。