如何访问 XtraGrid 列中的存储库组合框值
How to access repository combobox value in XtraGrid column
我有一个定义了 3 列的 XtraGrid GridControl,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")
returns null
。我可能错过了什么?这是解决这个问题的正确方法吗?
ComboBox
编辑器仅在焦点单元格处于编辑状态时存在。因此,ComboBox
在您激活它时创建,并在您离开时销毁。因此,当您按下 Button
时,您的 GridView
中没有 ComboBox
。
如果您想获得 ComboBox
值,那么您需要使用 RepositoryItemComboBox
的事件或编辑 GridView
的事件。编辑器的 link 存储在 RepositoryItemComboBox
事件的 sender
参数和 GridView.ActiveEditor
property. The value of the ComboBox
you can get by using ComboBox.EditValue
property or by using GridView.EditingValue
属性 中。您也可以通过使用 GridView
编辑事件的 EventArgs
参数来获取值,例如 ValidatedEditor
、CellValueChanging
、CellValueChanged
.
例如:
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];
}
我决定将列更改为绑定字段,在填充网格时传入一个空字符串,现在我可以通过以下方式访问数据:
string Test = gridView1.GetRowCellValue(i, "test").ToString();
不想这样做,但效果很好...
我有一个定义了 3 列的 XtraGrid GridControl,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")
returns null
。我可能错过了什么?这是解决这个问题的正确方法吗?
ComboBox
编辑器仅在焦点单元格处于编辑状态时存在。因此,ComboBox
在您激活它时创建,并在您离开时销毁。因此,当您按下 Button
时,您的 GridView
中没有 ComboBox
。
如果您想获得 ComboBox
值,那么您需要使用 RepositoryItemComboBox
的事件或编辑 GridView
的事件。编辑器的 link 存储在 RepositoryItemComboBox
事件的 sender
参数和 GridView.ActiveEditor
property. The value of the ComboBox
you can get by using ComboBox.EditValue
property or by using GridView.EditingValue
属性 中。您也可以通过使用 GridView
编辑事件的 EventArgs
参数来获取值,例如 ValidatedEditor
、CellValueChanging
、CellValueChanged
.
例如:
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];
}
我决定将列更改为绑定字段,在填充网格时传入一个空字符串,现在我可以通过以下方式访问数据:
string Test = gridView1.GetRowCellValue(i, "test").ToString();
不想这样做,但效果很好...