【问题标题】:How to access repository combobox value in XtraGrid column如何访问 XtraGrid 列中的存储库组合框值
【发布时间】:2015-10-30 09:50:26
【问题描述】:

我定义了一个 XtraGrid GridControl,其中包含 3 列、2 个数据绑定和一个我已设置为 RepositoryItemComboBox 的列。该列的设置如下:

this.gridColumn3.Caption = "Test";
this.gridColumn3.FieldName = "test";
this.gridColumn3.Name = "gridColumn3";
this.gridColumn3.UnboundType = DevExpress.Data.UnboundColumnType.String;
this.gridColumn3.Visible = true;
this.gridColumn3.VisibleIndex = 2;

RepositoryItemComboBox 的创建方式如下:

RepositoryItemComboBox cbo = new RepositoryItemComboBox();

cbo.Items.Add("1");
cbo.Items.Add("2");
cbo.Items.Add("3");
cbo.Items.Add("4");
cbo.Items.Add("5");
cbo.Items.Add("6");
cbo.Items.Add("7");

gridView1.Columns[3].ColumnEdit = cbo;

查看网格时,combobox 完全按照我的意愿显示。尝试检索在combobox 中选择的值时会出现此问题。当按下网格外的button 时,应处理combobox 值。我使用以下代码:

for (int i = 0; i < gridView1.DataRowCount; i++)
{
    int ID = (int)gridView1.GetRowCellValue(i, "id");
    string Test = gridView1.GetRowCellValue(i, "test").ToString();

    ProcessCombo(ID, Test);
}

在上面的代码中ID按预期检索,但gridView1.GetRowCellValue(i, "test")返回null。我可能错过了什么?这甚至是解决这个问题的正确方法吗?

【问题讨论】:

  • 如果出现您将列设置为未绑定列,在这种情况下,需要处理 GridView 的 CustomUnboundColumnData 事件以便为单元格提供值。
  • 我已经看过了,但是如何访问 CustomUnboundColumnData 事件中组合框的值?

标签: c# combobox devexpress xtragrid


【解决方案1】:

ComboBox 编辑器仅在焦点单元格处于编辑状态时存在。因此,ComboBox 在您激活时创建,在您离开时销毁。因此,当您按下Button 时,您的GridView 中将没有ComboBox

如果您想获得ComboBox 的值,则需要使用RepositoryItemComboBox 的事件或编辑GridView 的事件。编辑器的链接存储在RepositoryItemComboBox 事件的sender 参数和GridView.ActiveEditor 属性中。您可以使用ComboBox.EditValue 属性或GridView.EditingValue 属性获取ComboBox 的值。您也可以通过使用GridView 编辑事件的EventArgs 参数来获取值,例如ValidatedEditorCellValueChangingCellValueChanged

例如:

private void RepositoryItemComboBox1_Closed(object sender, ClosedEventArgs e)
{
    //Get the value from sender:
    var comboBox = (ComboBoxEdit)sender;
    object value = comboBox.EditValue;

    //Get the value from active editor:
    object activeEditorValue = gridView1.ActiveEditor.EditValue;

    //Get the value from editing value:
    object editingValue = gridView1.EditingValue;
}

另外,如果您想存储您的值并在以后使用它,那么您可以使用ColumnView.CustomUnboundColumnData 事件。

示例如下:

private Dictionary<int, string> _comboBoxValues = new Dictionary<int, string>();

private void gridView1_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
{
    if (e.Column.FieldName != "test")
        return;

    int id = (int)gridView1.GetListSourceRowCellValue(e.ListSourceRowIndex, "id");

    if (e.IsSetData)
        _comboBoxValues[id] = (string)e.Value;
    else if (e.IsGetData && _comboBoxValues.ContainsKey(id))
        e.Value = _comboBoxValues[id];
}

【讨论】:

  • 那么如何将此值应用于单元格并在按下按钮时检索它?
  • @anothershrubery 您不能将此值直接应用于单元格。 GridView 内没有存储单元格值。对于绑定列,单元格的值是从您分配给GridControl 的数据源中检索的。对于未绑定的列,值由GridColumn.UnboundExpression 属性计算或在GridView.CustomUnboundColumnData 事件中定义。
  • 是的,所以它没有回答这个问题?我知道如何获取上述值,但需要通过在网格外按下按钮来访问它们。
  • @anothershrubery 所以,不清楚你在问什么。您的问题是关于«如何从 ComboBox 中获取价值?»或 «如何将 ComboBox(或其他编辑器)中的值存储在未绑定的列中?»?
  • @anothershrubery 我添加了使用ColumnView.CustomUnboundColumnData 事件存储和检索值的示例。
【解决方案2】:

我决定将列更改为绑定字段,在填充网格时传入一个空字符串,现在我可以通过以下方式访问数据:

string Test = gridView1.GetRowCellValue(i, "test").ToString();

不想这样做,但它确实有效......

【讨论】:

    猜你喜欢
    • 2010-09-16
    • 1970-01-01
    • 2022-07-22
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 2010-09-16
    相关资源
    最近更新 更多