KB Article T904183
Visible to All Users

Access, Modify and Validate Rows and Cells of a Data-Aware Control - WinForms Cheat Sheet

Other DevExpress WinForms Cheat Sheets


This article demonstrates API used throughout DevExpress WinForms data-aware controls (Gantt Control, Data Grid, Tree List, Vertical Grid, etc.) to access, modify, edit and validate rows/cell values.

Obtain a cell's real or display values Obtain an object that represents the specified row Modify cell values Validate cell values Validate rows Prevent cells from being edited
Data Grid (1) GetRowCellValue, GetRowCellDisplayText, GetFocusedRowCellValue, GetFocusedRowCellDisplayText GetRow / GetDataRow (2) SetRowCellValue ValidatingEditor ValidateRow ShowingEditor
Tree List/Gantt Control GetRowCellValue, GetRowCellDisplayText, GetFocusedRowCellValue GetRow / GetDataRow, GetDataRecordByNode SetRowCellValue ValidatingEditor ValidateNode ShowingEditor
Vertical Grid GetCellValue, GetCellDisplayText GetRecordObject SetCellValue ValidatingEditor ValidateRecord ShowingEditor
Scheduler Control Appointment.GetValue Appointment.GetRow Appointment.SetValue (3) (3) (3)

1 - When runs in Master-Detail Mode, detail data is shown inside clone Views: dynamically created copies of pattern Views. Pattern views hold no data and API listed in this table has no effect when used from this View. Call the GetDetailView method to obtain a clone, and utilize aforementioned API for the returned View. See the following topic for more information: Working with Master-Detail Relationships in Code.

2 - To retrieve detail rows for several master rows, we recommend using methods of the underlying data source.

3 - To validate or restrict user input in the Edit Appointment form, you must create a Custom Appointment Edit Form and perform editor validation there.

Examples

Avoid duplicating values in columns (Data Grid)
  • Handle the ValidatingEditor or ValidateRow event
  • In the event handler, traverse all rows and compare cell values
  • Return false if the value is not unique
C#
private void GridView_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e) { GridView view = sender as GridView; e.Valid = ValidateValue(view, view.FocusedColumn.FieldName, e.Value); } private bool ValidateValue(GridView view, string fieldName, object value) { int dataRowCount = view.DataRowCount; for (int i = 0; i < dataRowCount; i++) { object cellValue = view.GetRowCellValue(i, fieldName); if (cellValue.Equals(value)) return false; } return true; }
Do not allow users to edit a row if a corresponding editor is empty (Data Grid)
  • Handle the ShowingEditor event
  • Set the e.Cancel parameter to false if the editor has no value
C#
private void GridView_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = string.IsNullOrEmpty((string)textEdit.EditValue); }
Allow editing individual cells in a read-only control (Data Grid)
  • Do not disable global control settings (ReadOnly, AllowEdit) and leave the control editable
  • Handle the ShowingEditor event and set the e.Cancel parameter to false for all cells except those that should remain editable
C#
private void gridView1_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e) { GridView View = sender as GridView; string cellValue = View.GetRowCellValue(View.FocusedRowHandle, colCountry).ToString(); if (cellValue == "Germany") e.Cancel = true; }
Read a cell value by its row and column indexes (Data Grid)
  • Obtain a row handle
  • Call the GetRowCellValue method
C#
string cellValue; cellValue = gridView1.GetRowCellValue(2, "ID").ToString();
Get an object associated with the clicked row (Data Grid)
C#
Record rec = gridView1.GetFocusedRow() as Record;
Get all visible rows (Data Grid)
  • Iterate through rows with the GetRow method. The last row index can be obtained from the RowCount property. See the following "Rows" article section for more details: > Traversing Rows
C#
for (int i = 0; i < gridView.RowCount; i++) { var row = gridView.GetRow(i); //... }
Get or set New Item Row cell values (Data Grid)
  • Handle the InitNewRow event that fires when a new row is added (learn more)
C#
private void GridView_InitNewRow(object sender, InitNewRowEventArgs e) { var view = sender as GridView; var value = view.GetRowCellValue(e.RowHandle, "FieldName"); }
Access detail view records (Data Grid)
  • Call the GetDetailView method to retrieve a clone View that holds detail data
  • Use the same API as for regular (non master-detail) binding modes
C#
//expand a row gridView.ExpandMasterRow(rowHandle); //get clone view GridView view = gridView.GetDetailView(rowHandle, relationIndex) as GridView; if (view != null) { //use a clone view methods var row = view.GetRow(detailRowHandle); }
Update records in the detail view (Data Grid)
  • Call the GetDetailView method to retrieve a clone View that holds detail data
  • Call PostEditor or CloseEditor methods
  • Call UpdateCurrentRow to manually trigger the row update (learn more)
C#
//get clone view GridView view = gridView.GetDetailView(rowHandle, relationIndex) as GridView; if (view != null) { //use a clone view methods view.PostEditor(); }
Update values of unbound nodes in TreeList
  • Use the TreeList.Nodes and TreeListNode.Nodes collections (or FindNode~ methods) to access a required node
  • Call the SetRowCellValue method to set a value
C#
TreeListNode rootNode = treeList.Nodes[0]; //get a root node TreeListNode childNode = rootNode.Nodes[0]; //get a child node TreeListNode currentNode = treeList.FindNodeByFieldValue("ID", 6); //find a node with ID = 6 //set a node value treeList.SetRowCellValue(rootNode, "ID", 0); treeList.SetRowCellValue(childNode, "ID", 10);
Modify values of EditForm editors or implement your own editing logic

Option #1:

  • Create a custom EditForm as described in this help topic: Create Custom Edit Forms
  • Modify editors behavior and/or assign values as you would do with regular standalone editors

Option #2:

How to set a mask (for instance, limit the maximum value length) for an in-place or Edit Form editor (Data Grid)
  • Create a custom editor with required settings: Input Mask, MaxLength and others.
  • Use the ColumnEdit property to replace a default editor with your custom one
C#
gridView.OptionsBehavior.EditingMode = GridEditingMode.EditForm; RepositoryItemTextEdit edit = new RepositoryItemTextEdit(); edit.MaxLength = 4; gridView.Columns["Text"].ColumnEdit = edit;
Set row cell values in accordance to value selected in another column's lookup editor (Data Grid)
  • Handle the EditValueChanged event and retrieve a lookup editor. If this editor is inside a New Item Row, use the IsNewItemRow property to identify this row is selected
  • Use the GetSelectedDataRow method to retrieve the data object selected in this lookup editor
  • Call SetRowCellValue to set the required cell value
C#
repositoryItemGridLookUpEdit.EditValueChanged += RepositoryItemGridLookUpEdit_EditValueChanged; private void RepositoryItemGridLookUpEdit_EditValueChanged(object sender, EventArgs e) { if (gridView.IsNewItemRow(gridView.FocusedRowHandle)) { GridLookUpEdit gle = sender as GridLookUpEdit; SpaceObject so = gle.GetSelectedDataRow() as SpaceObject; gridView.SetFocusedRowCellValue("Name", so.Name); } }
How to finish cell editing and stop displaying the edit icon in a row
  • When you start editing a cell, a row goes into edit mode and stays in it until focus is moved to another row. Then, a Data-Aware control validates the whole row. To stimulate this process, you can invoke TreeList.EndCurrentEdit/ColumnView.UpdateCurrentRow in the HiddenEditor event handler:
C#
private void treeList1_HiddenEditor(object sender, EventArgs e){ TreeList treeList = sender as TreeList; treeList.EndCurrentEdit(); } private void GridView1_HiddenEditor(object sender, EventArgs e){ var view = sender as GridView; view.UpdateCurrentRow(); }

Help us improve this article

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.