Cells
- 4 minutes to read
The VGridControl arranges data in rows and columns. A single column corresponds to a single record in the data source. If a row corresponds to a single data field, there is only one cell in an intersection of a row and column. If a single row contains multiple data source fields, there are multiple cells in an intersection of a row and column. This topic shows how to obtain or set a cell’s value.
How to Identify a Cell
Use the GetCellValue or SetCellValue method to obtain or set a cell value. To identify the cell, use the field name (which specifies a row) and record index (which specifies a column). You can also use an EditorRow, MultiEditorRow, or RowProperties object to specify a row. For multi-editor rows (which display multiple cells in a single column), you should also specify the cell index. The image below illustrates cells in a single- and multi-editor row.
// Cell is identified by a field name and record index.
vGridControl.SetCellValue(fieldName: "Name", recordIndex: 0, value: "Touareg");
// Cell is identified by a single-editor row object and record index.
vGridControl.SetCellValue(gridRow: erName, recordIndex: 0, value: "Touareg");
// Cell is identified by a multi-editor row object, record index and cell index.
vGridControl.SetCellValue(meRow: merTransmmision, recordIndex: 0, cellIndex: 1, value: 8);
// Cell is identified by a row properties object and record index.
vGridControl.SetCellValue(props: merpTransmissionSpeeds, recordIndex: 0, value: 8);
If you obtain or set the focused row’s value, check if the focused row is an EditorRow or MultiEditorRow, and then use the method’s corresponding overload.
string focusedCellValue = string.Empty;
if (vGridControl1.FocusedRow is DevExpress.XtraVerticalGrid.Rows.EditorRow)
focusedCellValue = vGridControl1.GetCellValue(vGridControl1.FocusedRow, vGridControl1.FocusedRecord).ToString();
Important
If the specified value’s type and the data field’s type do not match, the InvalidValueException event is raised. See the following topic for more information: Validation.
Edited Cell
If a cell is being edited, you can use the EditingValue property to obtain the cell’s value. Before you use this property, check to see if the ActiveEditor property equals to null (Nothing in VB.NET) or not. Otherwise, setting a value to the EditingValue property has no effect.
if (vGridControl1.ActiveEditor != null) {
string cellValue = vGridControl1.EditingValue.ToString();
if (cellValue != "")
vGridControl1.EditingValue = "";
}
Unbound Mode
If the control is not bound to a data source, it can only display a single value in each row. To specify a row’s value, use its Value property.
The code below handles the CellValueChanged event to calculate a cell’s value based on values in other cells. The Real Price row’s value is the product of the Price and Discount row values.
private void vGridControl1_CellValueChanged(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e) {
if (e.Row == rowPriceAndDiscount) {
int price = Convert.ToInt32(rowPriceAndDiscount.PropertiesCollection[0].Value);
double discount = Convert.ToDouble(rowPriceAndDiscount.PropertiesCollection[1].Value);
rowRealPrice.Properties.Value = price * discount;
}
}