windowsforms-devexpress-dot-xtragrid-dot-views-dot-base-dot-columnview-cede53f4.md
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
[Browsable(false)]
[XtraSerializableProperty(XtraSerializationVisibility.Collection, true, true, true, 0, XtraSerializationFlags.DefaultValue)]
[XtraSerializablePropertyId(2)]
public virtual GridColumnCollection Columns { get; }
<Browsable(False)>
<XtraSerializableProperty(XtraSerializationVisibility.Collection, True, True, True, 0, XtraSerializationFlags.DefaultValue)>
<XtraSerializablePropertyId(2)>
Public Overridable ReadOnly Property Columns As GridColumnCollection
| Type | Description |
|---|---|
| GridColumnCollection |
A GridColumnCollection object representing a collection of available columns within the current View.
|
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.
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
});
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.
|
View Type
|
Column Type
| | --- | --- | |
|
| |
|
| |
|
|
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
The following example demonstrates how to create a column within a View, and assign a specific editor to it.
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;
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
protected void ExpandRowByRowInfo(RowInfo rowInfo){
int dataRowHandle = view.LocateByValue(0, view.Columns[keyFieldName], rowInfo.Id);
if(dataRowHandle != GridControl.InvalidRowHandle){
void ExportColumns(IXlSheet sheet) {
foreach (GridColumn column in this.view.Columns)
ExportColumn(sheet, column);
modelColumnExtender.GroupFooterSummaryType != SummaryItemType.None) {
GridColumn gridColumn = gridView.Columns[
modelColumn.ModelMember.MemberInfo.BindingName];
}
GridColumn buttonColumn = gridListEditor.GridView.Columns[unboundModelColumn.PropertyName];
if(buttonColumn != null) {
winforms-grid-initialize-new-item-row-on-start-editing/CS/WindowsApplication73/Form1.cs#L23
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
Protected Sub ExpandRowByRowInfo(ByVal rowInfo As RowInfo)
Dim dataRowHandle As Integer = view.LocateByValue(0, view.Columns(keyFieldName), rowInfo.Id)
If dataRowHandle <> GridControl.InvalidRowHandle Then
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
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
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
Dim formatConditionRuleValue As New FormatConditionRuleValue()
gridFormatRule.Column = gridView1.Columns("RequiredDate")
formatConditionRuleValue.PredefinedName = "Red Bold Text"
See Also
Tutorial: Working with Columns in Code