Back to Devexpress

Cells

windowsforms-425-controls-and-libraries-vertical-grid-data-layout-records-rows-and-cells-cells.md

latest7.4 KB
Original Source

Cells

  • Sep 03, 2021
  • 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.

csharp
// 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);
vb
' Cell is identified by a field name and record index.
vGridControl1.SetCellValue(feildName:= "Name", recordIndex:= 0, value:= "Touareg")
' Cell is identified by a single-editor row object and record index.
vGridControl1.SetCellValue(gridRow:= erName, recordIndex:= 0, value:= "Touareg")
' Cell is identified by a multi-editor row object, record index and cell index.
vGridControl1.SetCellValue(meRow:= merTransmmision, recordIndex:= 0, cellIndex:= 1, value:= 8)
' Cell is identified by a row properties object and record index.
vGridControl1.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.

csharp
string focusedCellValue = string.Empty;
if (vGridControl1.FocusedRow is DevExpress.XtraVerticalGrid.Rows.EditorRow)
   focusedCellValue = vGridControl1.GetCellValue(vGridControl1.FocusedRow, vGridControl1.FocusedRecord).ToString();
vb
Dim focusedCellValue As String = String.Empty
If TypeOf VGridControl1.FocusedRow Is DevExpress.XtraVerticalGrid.Rows.EditorRow Then
    focusedCellValue = VGridControl1.GetCellValue(VGridControl1.FocusedRow, VGridControl1.FocusedRecord).ToString()
End If

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.

csharp
if (vGridControl1.ActiveEditor != null) {
   string cellValue = vGridControl1.EditingValue.ToString();
   if (cellValue != "")
      vGridControl1.EditingValue = "";
}
vb
If Not VGridControl1.ActiveEditor Is Nothing Then
    Dim cellValue As String = VGridControl1.EditingValue.ToString()
    If cellValue <> "" Then
        VGridControl1.EditingValue = ""
    End If
End If

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.

csharp
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;
   }
}
vb
Private Sub VGridControl1_CellValueChanged(ByVal sender As Object, ByVal e As DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs) _
Handles VGridControl1.CellValueChanged
    Dim price As Integer
    Dim discount As Integer
    If e.Row = rowPriceAndDiscount Then
        price = Convert.ToInt32(rowPriceAndDiscount.PropertiesCollection(0).Value)
        discount = Convert.ToDouble(rowPriceAndDiscount.PropertiesCollection(1).Value)
        rowRealPrice.Properties.Value = price * discount
    End If
End Sub

See Also

Appearances

Customizing Appearances of Individual Rows

Customizing Appearances of Individual Cells

Disabled Cell Behavior