Back to Devexpress

GridColumn.FieldName Property

windowsforms-devexpress-dot-xtragrid-dot-columns-dot-gridcolumn-230a8987.md

latest10.2 KB
Original Source

GridColumn.FieldName Property

To bind a column to a data source field, set this property to the required data field name. To create unbound columns, assign a unique identifier to this property. If you obtained a column as a method return value or as an event argument, read the FieldName property value to identify this column.

Namespace : DevExpress.XtraGrid.Columns

Assembly : DevExpress.XtraGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
[DefaultValue("")]
[DXCategory("Data")]
[XtraSerializableProperty]
public string FieldName { get; set; }
vb
<DXCategory("Data")>
<DefaultValue("")>
<XtraSerializableProperty>
Public Property FieldName As String

Property Value

TypeDefaultDescription
StringString.Empty

A String value that specifies the unique column field name. The value is case-sensitive.

|

Remarks

The FieldName property does not affect the column caption.

Important

  • Assign unique FieldName values for unbound columns. Otherwise, the ColumnView.CustomUnboundColumnData event is unable to find required columns.
  • For data-bound records, it is not recommended to bind multiple columns to the same data field because it limits and/or alters Data Grid functionality: you cannot access a specific column by field name, an update to a single cell value affects all related columns, unexpected behavior when records are sorted, filtered or grouped, etc.

The following sample shows how to obtain a column by its FieldName and change the column’s appearance. To test this code sample, run the following Demo Center module: Prioritize conditional formatting appearances.

csharp
FormatConditionRuleValue lengthRuleCondition = new FormatConditionRuleValue();
lengthRuleCondition.Condition = FormatCondition.GreaterOrEqual;
lengthRuleCondition.Value1 = 25;
lengthRuleCondition.Appearance.BackColor = Color.MediumSeaGreen;
lengthRuleCondition.Appearance.Options.UseBackColor = true;
GridFormatRule lengthRule = new GridFormatRule() { Column = gridView.Columns["Length"], Rule = lengthRuleCondition };
gridView.FormatRules.Add(lengthRule);

gridView.RowCellStyle += (s, e) => {
    GridView view = s as GridView;
    if (view.IsRowSelected(e.RowHandle) &&
        e.Column.FieldName == "Length" &&
        lengthRule.IsFit(e.CellValue, view.GetDataSourceRowIndex(e.RowHandle)))
    {
        AppearanceObject ruleAppearance = (lengthRule.Rule as FormatConditionRuleAppearanceBase).Appearance;
        e.Appearance.BackColor = ruleAppearance.BackColor;
    }
};
vb
Dim lengthRuleCondition As New FormatConditionRuleValue()
lengthRuleCondition.Condition = FormatCondition.GreaterOrEqual
lengthRuleCondition.Value1 = 25
lengthRuleCondition.Appearance.BackColor = Color.MediumSeaGreen
lengthRuleCondition.Appearance.Options.UseBackColor = True
Dim lengthRule As New GridFormatRule() With {.Column = gridView.Columns("Length"), .Rule = lengthRuleCondition}
gridView.FormatRules.Add(lengthRule)

AddHandler gridView.RowCellStyle, Sub(s, e)
    Dim view As GridView = TryCast(s, GridView)
    If view.IsRowSelected(e.RowHandle) AndAlso e.Column.FieldName = "Length" AndAlso lengthRule.IsFit(e.CellValue, view.GetDataSourceRowIndex(e.RowHandle)) Then
        Dim ruleAppearance As AppearanceObject = (TryCast(lengthRule.Rule, FormatConditionRuleAppearanceBase)).Appearance
        e.Appearance.BackColor = ruleAppearance.BackColor
    End If
End Sub

Bind Grid Columns to Nested Properties

You can also bind a column to a nested property. To do this, assign parent and child property names (separated by the dot character - “.”) to the FieldName property.

csharp
column.FieldName = "ParentProperty.NestedProperty";
vb
column.FieldName = "ParentProperty.NestedProperty

The following code snippets (auto-collected from DevExpress Examples) contain references to the FieldName property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-reporting-create-grid-based-report/CS/ConvertGridToReportExample/XtraReport1.cs#L45

csharp
if (gridColumn.SortOrder == ColumnSortOrder.Ascending)
    gField = new GroupField(gridColumn.FieldName, XRColumnSortOrder.Ascending);
else

winforms-grid-show-editor-buttons-on-cell-hover/CS/WindowsApplication3/Form1.cs#L31

csharp
if(hi.InRowCell && hi.Column.FieldName == "Text")
    rowHandle = hi.RowHandle;

XAF-how-to-add-an-unbound-column-to-gridlisteditor-to-execute-a-custom-action-for-a-record/CS/EFCore/ButtonInListEF/ButtonInListEF.Win/Controllers/SimpleBusinessActionGridListViewController.cs#L91

csharp
private void gridView_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e) {
    if(e.Column.FieldName == ButtonColumnName) {
        ISimpleBusinessAction order = gridListEditor.GridView.GetRow(e.RowHandle) as ISimpleBusinessAction;

winforms-grid-display-pictures-from-field-with-image-paths/CS/Form1.cs#L229

csharp
private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) {
    if(e.Column.FieldName == "Image" && e.IsGetData) {
        GridView view = sender as GridView;

winforms-custom-buttonedit-display-editvalue-on-button/CS/TestMyButtonEdit/Form1.cs#L37

csharp
{
    if (e.Column.FieldName == "Info")
        e.RepositoryItem = repositoryItemMBE;

winforms-reporting-create-grid-based-report/VB/ConvertGridToReportExample/XtraReport1.vb#L43

vb
If gridColumn.SortOrder = ColumnSortOrder.Ascending Then
    gField = New GroupField(gridColumn.FieldName, XRColumnSortOrder.Ascending)
Else

winforms-grid-show-editor-buttons-on-cell-hover/VB/WindowsApplication3/Form1.vb#L33

vb
If hi.InRowCell AndAlso hi.Column.FieldName = "Text" Then
    rowHandle = hi.RowHandle

winforms-grid-display-pictures-from-field-with-image-paths/VB/Form1.vb#L218

vb
Private Sub gridView1_CustomUnboundColumnData(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs)
    If Equals(e.Column.FieldName, "Image") AndAlso e.IsGetData Then
        Dim view As GridView = TryCast(sender, GridView)

winforms-custom-buttonedit-display-editvalue-on-button/VB/TestMyButtonEdit/Form1.vb#L38

vb
Private Sub gridV_CustomRowCellEdit(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles gridV.CustomRowCellEdit
    If e.Column.FieldName = "Info" Then
        e.RepositoryItem = repositoryItemMBE

winforms-grid-display-edit-rtf-data/VB/Form1.vb#L49

vb
Private Sub gridView1_CustomRowCellEditForEditing(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs)
    If Equals(e.Column.FieldName, "Rich") Then e.RepositoryItem = riPopup
End Sub

See Also

Unbound Columns

GridColumn Class

GridColumn Members

DevExpress.XtraGrid.Columns Namespace