Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Collapse]WinForms Controls
  Prerequisites
 [Expand]What's Installed
 [Expand]Build an Application
 [Collapse]Controls and Libraries
  [Expand]Forms and User Controls
  [Expand]Messages, Notifications and Dialogs
  [Expand]Editors and Simple Controls
  [Expand]Ribbon, Bars and Menu
  [Expand]Application UI Manager
  [Expand]Docking Library
  [Collapse]Data Grid
   [Expand]Getting Started
   [Expand]Binding to Data
   [Expand]Grid View
   [Expand]View Technology
   [Expand]Grouping
   [Expand]Sorting
   [Expand]Summaries
   [Collapse]Data Editing
    [Expand]Edit Form
    [Collapse]Editing via Code
      Adding and Deleting Records
      Obtaining and Setting Cell Values
      Refreshing the GridControl
    [Expand]In-place Editors
    [Expand]Input Validation
    [Expand]New Item Row
     Tutorial: Add or Remove Rows
     Tutorial: Data Input Validation
   [Expand]Filtering and Locating Rows
   [Expand]Focus and Selection Handling
   [Expand]Processing Rows
   [Expand]Formatting Cell Values
   [Expand]Master-Detail Relationships
   [Expand]Asynchronous Image Load
   [Expand]Export and Printing
   [Expand]Appearance and Conditional Formatting
   [Expand]Batch Modifications
   [Expand]Hit Information
   [Expand]Hints
   [Expand]Popup Menus
   [Expand]Saving and Restoring Layouts
   [Expand]Visual Elements
   [Expand]Design-Time Features
   [Expand]Examples
   [Expand]End-User Capabilities
    Included Components
  [Expand]Vertical Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Chart Control
  [Expand]Diagrams
  [Expand]Gauges
  [Expand]Map Control
  [Expand]Scheduler
  [Expand]Spreadsheet
  [Expand]Rich Text Editor
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]Printing-Exporting
  [Expand]PDF Viewer
   Reporting
  [Expand]Snap
  [Expand]TreeMap Control
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Reporting
[Expand]Report Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Obtaining and Setting Cell Values

You can get and set the values of cells using the methods provided by a View or the bound data source. The first approach allows you to write portable code that can be applied to different data sources. The second one gives quicker access to the values of cells.

Those data sources that do not implement the System.ComponentModel.IBindingList interface, do not support change notifications. Thus, if you change the values of cells using such a data source, the grid control will not reflect the changes made and you will need to update the grid manually. For more information, refer to the Refreshing the GridControl document.

To get or set cell value, identify the cell by the row and column in which it resides. Rows are identified by their handles (which can be obtained as described in the Identifying Rows and Cards, Traversing Rows and Locating Rows in Code topics). The Accessing and Identifying Columns topic describes how to get the desired column object.

This topic consists of the following sub-topics:

Expanded Get Cell Values

Grid Control provides the following methods for obtaining cell values.

Method Description
ColumnView.GetFocusedValue

ColumnView.FocusedValue

Returns the edit value within the currently focused cell.

The focused cell is identified by the focused column, which is specified by the ColumnView.FocusedColumn property, and the focused row is specified by the ColumnView.FocusedRowHandle property.

ColumnView.GetFocusedRowCellValue Returns the edit value of a specific column within the focused row.

The focused row is identified by the ColumnView.FocusedRowHandle property.

ColumnView.GetRowCellValue Returns the edit value of any cell within a View. The target row is identified by its index in the View.
ColumnView.GetListSourceRowCellValue Returns a cell value from a row in the data source. The target row is identified by its index in the data source.
ColumnView.EditingValue Returns the currently edited value. If the cell isn't being edited, this property returns null (Nothing in Visual Basic).

You can use the BaseView.IsEditing property to determine whether a cell is currently being edited or not. To get the currently active editor, use the BaseView.ActiveEditor property.

The column for the ColumnView.GetFocusedRowCellValue and ColumnView.GetRowCellValue methods can be identified by a specific GridColumn object or by a string that represents a column's field name.

To obtain the value of a cell via the ColumnView.GetRowCellValue method, you also need to identify the row that contains the desired cell. The row is identified by a handle that represents the row's visual position within the View. For more information on row handles, refer to the Identifying Rows and Cards document.

All these methods return the object representation of a cell's value, so type conversions should be carried out if required.

Expanded Example 1

The following sample code can be used to get the value of the ID column within the third data row (this row's handle is equal to 2).

 

The following methods can be used to get the displayed values of cells. The GridColumn.DisplayFormat property can be used to specify how edit values should be converted into display values. For information on formatting, see the Formatting Cell Values document.

Method Description
ColumnView.GetFocusedDisplayText Returns a focused cell's display text.
ColumnView.GetFocusedRowCellDisplayText Returns a cell's display text within the focused row.
ColumnView.GetRowCellDisplayText Returns the displayed value of any cell within a View.

 

Expanded Example 2

The following sample code can be used to obtain the text displayed within a focused cell.

 

It is also possible to get the values of cells using the methods provided by the grid's data source. For instance, the ColumnView.GetRow, ColumnView.GetDataRow, ColumnView.GetFocusedRow and ColumnView.GetFocusedDataRow methods return objects that represent rows in a data source. After rows are obtained, use their methods to retrieve field values.

The ColumnView.GetRow and ColumnView.GetFocusedRow methods can be used regardless of the data source's type.

The ColumnView.GetDataRow and ColumnView.GetFocusedDataRow methods can be used if the View's data source is represented by a System.Data.DataTable or System.Data.DataView. In this case, rows are represented by System.Data.DataRow objects and the GetDataRow(GetFocusedDataRow) method returns the row typecasted to this data type.

 

Expanded Example 3

The following sample code can be used to get the value of the first column within the focused row.

Expanded Set Cell Values

To set the edit values of particular cells, the following View methods can be used.

Method Description
ColumnView.SetFocusedValue Assigns a value to the currently focused cell.
ColumnView.SetFocusedRowCellValue Assigns a value to a specific cell within the focused row.
ColumnView.SetRowCellValue Assigns a value to any cell within a View.
ColumnView.EditingValue Modifies the currently edited value. If the cell is not being edited, assigning a value to the EditingValue property has no effect.

You can use the BaseView.IsEditing property to determine whether or not a cell is being edited at the moment. To get the currently active editor, use the BaseView.ActiveEditor property.

Rows and columns in these methods are identified in the same manner as with the ColumnView.GetFocusedRowCellValue and ColumnView.GetRowCellValue methods (see above).

 

Expanded Example 4

The following sample code can be used to change the focused cell's value.

 

You can use the ColumnView.GetRow and ColumnView.GetDataRow methods to obtain objects that represent specific rows. Then, to change the values of the cells in the rows, use the functionality provided by your data rows.

 

Expanded Example 5

The following sample code can be used to set the value of the first column within the focused row.

 

Views provide several events to handle cell editing. When an end-user types or deletes a character within a cell, the ColumnView.CellValueChanging event is raised. After the value has been modified and the editor has been closed, the ColumnView.CellValueChanged event is generated. This event also occurs when cell values are changed in code. You can, for instance, handle this event if you need to synchronize other fields or controls with the changes made. If the values entered need to be validated, handle the validation events (BaseView.ValidatingEditor and ColumnView.ValidateRow). Refer to the Validating Editors and Validating Rows documents for additional information.

Expanded See Also

How would you rate this topic?​​​​​​​