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
- Handle the
ValidatingEditor
orValidateRow
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;
}
- 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);
}
- Do not disable global control settings (ReadOnly, AllowEdit) and leave the control editable
- Handle the
ShowingEditor
event and set thee.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;
}
- Obtain a row handle
- Call the
GetRowCellValue
method
C#string cellValue;
cellValue = gridView1.GetRowCellValue(2, "ID").ToString();
- Use the GridView.GetFocusedRow method.
C#Record rec = gridView1.GetFocusedRow() as Record;
- Iterate through rows with the
GetRow
method. The last row index can be obtained from theRowCount
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);
//...
}
- 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");
}
- 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);
}
- Call the
GetDetailView
method to retrieve a clone View that holds detail data - Call
PostEditor
orCloseEditor
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();
}
- Use the
TreeList.Nodes
andTreeListNode.Nodes
collections (orFindNode~
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);
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:
- Access Editors within the default Edit Form, and modify their properties/handle their events to implement custom behavior
- Handle the EditFormHidden event to remove references to the EditForm's controls and unsubscribe from the event handlers
- 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;
- 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);
}
}
- 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();
}