【问题标题】:Get the value of CheckedListBoxItem devexpress in gridview c#在gridview c#中获取CheckedListBoxItem devexpress的值
【发布时间】:2016-07-29 09:19:22
【问题描述】:

我在grivviewDevexpress 中添加了一个CheckedListBoxItem,您可以在此处看到:

如您所见,我在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"));
 }

【问题讨论】:

  • 您在“确认”列中存储了什么?正如我从列表的数据源中看到的那样。它应该具有与 User 的 Id 属性相似的数据类型。
  • @NiranjanKala 这是什么意思?我在问题中解释了

标签: c# gridview devexpress


【解决方案1】:

我可以怀疑您的代码将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() 获得的结果是User 类的Id 属性值,而不是CheckedListBoxItem

当您调用gridView.CloseEditor(); 时,将不存在编辑器来获取 CheckedListBoxItem。您可以通过ColumnView.ShownEditor Event 访问编辑器。见下面代码sn-p:

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

希望对您有所帮助..

【讨论】:

  • 感谢您的代码,但它不再工作它返回 null
  • 您的解决方案类似于我的建议,即您只能在选择后从网格单元中获取密钥。您可以使用这些键从数据源中获取实际记录。如果您的列表允许多选,那么您的问题与您的问题文本无关。没有人能判断你想从列表中获取多个值。很高兴你解决了这个问题
【解决方案2】:

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

【讨论】:

  • 我应该使用哪种投射?
猜你喜欢
  • 2012-12-17
  • 2021-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多