Skip to main content
A newer version of this page is available. .

Obtaining and Setting Cell Values

  • 4 minutes to read

The vertical grids (VGridControl and PropertyGridControl) provide a number of ways to obtain and set cell values. This topic provides a list of all methods with examples. The common task that needs to be solved when accessing cell values is to identify the desired cell. The Cells Overview topic describes how this can be performed.

This topic has three sub-sections that describe various aspects of accessing cell values. The first is accessing the edited cell’s value. The obvious shortcoming of this approach is that it can be used only when the grid is in the editing state. However, only this way of accessing cell values enables you to modify a cell value without closing the cell’s editor. The second way to access cell values provides easy data modifications ability in unbound mode. Note that this way cannot be used with other modes. Lastly, the way to access cell values independently of any conditions is considered.

Important note: assigning a cell value of the wrong type raises an exception. You can provide a centralized control over such situations using the VGridControlBase.InvalidValueException event. Please refer to the Validation topic for information on how to do this.

Obtaining and Setting the Value of the Edited Cell

You can access the value of the cell being edited using the VGridControlBase.EditingValue property. Note that you must first check whether the grid is in the editing state before using this property. Otherwise it will return null (Nothing in Visual Basic) and assigning values to it will have no effect. To determine the current state of the vertical grid, you need to inspect the VGridControlBase.ActiveEditor property value. If this property value is a null reference, editing is not being performed at the moment.

The sample code below shows how to use the VGridControlBase.EditingValue property. It sets the edited cell’s value to an empty string if the cell contains any text.


if (vGridControl1.ActiveEditor != null) {
   string cellValue = vGridControl1.EditingValue.ToString();
   if (cellValue != "")
      vGridControl1.EditingValue = "";
}

Obtaining and Setting Values in Unbound Mode

When a vertical grid works in unbound mode, each row item contains a single cell. The control is not bound to any data source and cell values are set using the RowProperties.Value property of the owning row items.

The sample code below shows how to use the RowProperties.Value property to access cell values. It assumes that the vertical grid control works in unbound mode and there are two rows within it. The first is the multi-editor row named rowPriceAndDiscount. It displays two cells representing the price of a product and the discount established. The second row is the editor row named rowRealPrice. It displays the price of the product taking the discount into account (the supposed layout of the control is shown in the image after the code). The sample code handles the VGridControlBase.CellValueChanged event. It is used to update the real price when either the product price or the discount is changed.


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

SetCellValues - UnboundMode

The Universal Way to Obtain and Set Cell Values

You can access cell values using the VGridControlBase.GetCellValue and VGridControlBase.SetCellValue methods. Both these methods have a number of overloads. The supplied overloads differ in how the accessed cell is specified.

The sample below shows how to obtain the string representation of the focused cell’s value. The code uses the VGridControlBase.GetCellValue method overload that accepts the row and the record index as the parameters. Note that identifying a cell using only these two parameters implies that the cell resides within an editor row. Thus, row type checking is required to avoid errors.


string focusedCellValue = string.Empty;
if (vGridControl1.FocusedRow is DevExpress.XtraVerticalGrid.Rows.EditorRow)
   focusedCellValue = vGridControl1.GetCellValue(vGridControl1.FocusedRow, vGridControl1.FocusedRecord).ToString();

The next sample code line shows how to set cell values using the VGridControlBase.SetCellValue method. It assigns zero to the cell residing within the focused record and belonging to the second item of the rowMPG row.


int cellValue = 0;
int cellIndex = 1;
vGridControl1.SetCellValue(rowMPG, vGridControl1.FocusedRecord, cellIndex, cellValue);