Back to Devexpress

ColumnView.Columns Property

windowsforms-devexpress-dot-xtragrid-dot-views-dot-base-dot-columnview-cede53f4.md

latest12.0 KB
Original Source

ColumnView.Columns Property

Provides access to the collection of columns available for display within the View.

Namespace : DevExpress.XtraGrid.Views.Base

Assembly : DevExpress.XtraGrid.v25.2.dll

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

Declaration

csharp
[Browsable(false)]
[XtraSerializableProperty(XtraSerializationVisibility.Collection, true, true, true, 0, XtraSerializationFlags.DefaultValue)]
[XtraSerializablePropertyId(2)]
public virtual GridColumnCollection Columns { get; }
vb
<Browsable(False)>
<XtraSerializableProperty(XtraSerializationVisibility.Collection, True, True, True, 0, XtraSerializationFlags.DefaultValue)>
<XtraSerializablePropertyId(2)>
Public Overridable ReadOnly Property Columns As GridColumnCollection

Property Value

TypeDescription
GridColumnCollection

A GridColumnCollection object representing a collection of available columns within the current View.

|

Remarks

Use the Columns property to access the collection of columns in the View. You can add, move, and delete columns. You can also access individual columns using indexer notation or by the bound field name.

csharp
using DevExpress.XtraGrid.Columns;

// Gets the first column in the Columns collection.
GridColumn colFirst = gridView1.Columns[0];
// Gets the column by its bound field name.
GridColumn colFirstName = gridView1.Columns["FirstName"];
// Adds a new column to the Columns collection.
gridView1.Columns.Add(new GridColumn() {
    Caption = "Last Name",
    FieldName = "LastName",
    Visible = true
});
vb
Imports DevExpress.XtraGrid.Columns

' Gets the first column in the Columns collection.
Private colFirst As GridColumn = gridView1.Columns(0)
' Gets the column by its bound field name.
Private colFirstName As GridColumn = gridView1.Columns("FirstName")
' Adds a new column to the Columns collection.
gridView1.Columns.Add(New GridColumn() With {.Caption = "Last Name", .FieldName = "LastName", .Visible = True})

In the CardView and LayoutView, columns are displayed as card fields. In the WinExplorerView, columns correspond to list item fields.

Column Types

|

View Type

|

Column Type

| | --- | --- | |

GridView

CardView

WinExplorerView

|

GridColumn

| |

AdvBandedGridView

BandedGridView

|

BandedGridColumn

| |

LayoutView

|

LayoutViewColumn

|

Read the following topic for detailed information and examples: Grid Columns.

Note

To display columns from the Columns collection in the WinExplorerView, assign them to the predefined WinExplorerView columns from ColumnSet.

Read the following topic for detailed information: WinExplorer View

Example

The following example demonstrates how to create a column within a View, and assign a specific editor to it.

csharp
using DevExpress.XtraGrid.Views.BandedGrid;
    using DevExpress.XtraEditors.Repository;
    //...
    // Create a column
    BandedGridColumn col = advBandedGridView1.Columns.Add("Country") as BandedGridColumn;
    // Add a column to the first Band
    advBandedGridView1.Bands[0].Columns.Add(col);
    // Show the new column
    col.Visible = true;
    // Create a Repository Item
    RepositoryItemLookUpEdit columnEditor = new RepositoryItemLookUpEdit();
    // Customize the editor
    //...
    // Add the new Repository Item to the "RepositoryItems" collection
    gridControl1.RepositoryItems.Add(columnEditor);
    // Assign the editor to the new column
    col.ColumnEdit = columnEditor;
vb
Imports DevExpress.XtraGrid.Views.BandedGrid
    Imports DevExpress.XtraEditors.Repository
    '...
    ' Create a column
    Dim col As BandedGridColumn = TryCast(advBandedGridView1.Columns.Add("Country"), BandedGridColumn)
    ' Add a column to the first Band
    advBandedGridView1.Bands(0).Columns.Add(col)
    ' Show the new column
    col.Visible = True
    ' Create a Repository Item
    Dim columnEditor As New RepositoryItemLookUpEdit()
    ' Customize the editor
    '...
    ' Add the new Repository Item to the "RepositoryItems" collection
    gridControl1.RepositoryItems.Add(columnEditor)
    ' Assign the editor to the new column
    col.ColumnEdit = columnEditor

The following code snippets (auto-collected from DevExpress Examples) contain references to the Columns 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-preserve-grid-state-on-refresh/CS/RefreshHelperClass.cs#L60

csharp
protected void ExpandRowByRowInfo(RowInfo rowInfo){
    int dataRowHandle = view.LocateByValue(0, view.Columns[keyFieldName], rowInfo.Id);
    if(dataRowHandle != GridControl.InvalidRowHandle){

winforms-grid-copy-cells-in-biff8-format-using-excel-export-api/CS/gridCopyToClipboardExample/CopyToClipboardHelper.cs#L21

csharp
void ExportColumns(IXlSheet sheet) {
    foreach (GridColumn column in this.view.Columns)
        ExportColumn(sheet, column);

xaf-how-to-extend-the-application-model/CS/EFCore/ExtendModelEF/ExtendModelEF.Win/Controllers/WinGroupFooterViewController.cs#L39

csharp
modelColumnExtender.GroupFooterSummaryType != SummaryItemType.None) {
GridColumn gridColumn = gridView.Columns[
    modelColumn.ModelMember.MemberInfo.BindingName];

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#L55

csharp
}
GridColumn buttonColumn = gridListEditor.GridView.Columns[unboundModelColumn.PropertyName];
if(buttonColumn != null) {

winforms-grid-initialize-new-item-row-on-start-editing/CS/WindowsApplication73/Form1.cs#L23

csharp
GridView view = sender as GridView;
view.SetRowCellValue(e.RowHandle, view.Columns["Name"], "Noname");
view.SetRowCellValue(e.RowHandle, view.Columns["Date"], DateTime.Today);

winforms-preserve-grid-state-on-refresh/VB/RefreshHelperClass.vb#L66

vb
Protected Sub ExpandRowByRowInfo(ByVal rowInfo As RowInfo)
    Dim dataRowHandle As Integer = view.LocateByValue(0, view.Columns(keyFieldName), rowInfo.Id)
    If dataRowHandle <> GridControl.InvalidRowHandle Then

winforms-grid-copy-cells-in-biff8-format-using-excel-export-api/VB/gridCopyToClipboardExample/CopyToClipboardHelper.vb#L23

vb
Private Sub ExportColumns(ByVal sheet As IXlSheet)
    For Each column As GridColumn In view.Columns
        ExportColumn(sheet, column)

winforms-grid-initialize-new-item-row-on-start-editing/VB/WindowsApplication73/Form1.vb#L18

vb
Dim view As GridView = TryCast(sender, GridView)
view.SetRowCellValue(e.RowHandle, view.Columns("Name"), "Noname")
view.SetRowCellValue(e.RowHandle, view.Columns("Date"), Date.Today)

winforms-grid-customize-cell-color/VB/Form1.vb#L34

vb
Private Sub simpleButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
    _CellColorHelper.SetCellColor(Convert.ToInt32(spinEdit1.Value), gridView1.Columns(Convert.ToInt32(spinEdit2.Value)), colorEdit1.Color)
End Sub

winforms-grid-bind-to-business-objects/VB/GridBoundToRuntimeCreatedData/Form1.vb#L41

vb
Dim formatConditionRuleValue As New FormatConditionRuleValue()
gridFormatRule.Column = gridView1.Columns("RequiredDate")
formatConditionRuleValue.PredefinedName = "Red Bold Text"

See Also

VisibleColumns

PopulateColumns()

GridColumn

Tutorial: Working with Columns in Code

Tutorial: Create and Manage Columns at Design Time

ColumnView Class

ColumnView Members

DevExpress.XtraGrid.Views.Base Namespace