windowsforms-devexpress-dot-xtragrid-dot-columns-dot-gridcolumn-3a11b1af.md
Gets or sets the column’s position within the View’s column header panel.
Namespace : DevExpress.XtraGrid.Columns
Assembly : DevExpress.XtraGrid.v25.2.dll
NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation
[DefaultValue(-1)]
[DXCategory("Appearance")]
[XtraSerializableProperty]
[XtraSerializablePropertyId(3)]
public virtual int VisibleIndex { get; set; }
<DXCategory("Appearance")>
<DefaultValue(-1)>
<XtraSerializableProperty>
<XtraSerializablePropertyId(3)>
Public Overridable Property VisibleIndex As Integer
| Type | Default | Description |
|---|---|---|
| Int32 | -1 |
A column’s position within the View. -1 for hidden columns.
|
If you disable a column’s GridColumn.Visible setting, this hidden column will store its last known VisibleIndex internally. This allows the column to automatically restore its previous position when you set the Visible property back to true.
int A = columnPrice.VisibleIndex; // A returns "5"
//hide the "Price" column
columnPrice.Visible = false;
int B = columnPrice.VisibleIndex; // B returns "-1"
//show the "Price" column again
columnPrice.Visible = true;
int C = columnPrice.VisibleIndex; // C returns "5"
Dim A As Integer = columnPrice.VisibleIndex ' A returns "5"
'hide the "Price" column
columnPrice.Visible = False
Dim B As Integer = columnPrice.VisibleIndex ' B returns "-1"
'show the "Price" column again
columnPrice.Visible = True
Dim C As Integer = columnPrice.VisibleIndex ' C returns "5"
Columns added by the GridColumnCollection.Add method are initially hidden. To add a column and show it immediatelly, call the GridColumnCollection.AddVisible method instead.
GridColumn col1 = gridView1.Columns.Add();
//col1.Visible returns "false"
//col1.VisibleIndex returns "-1"
GridColumn col2 = gridView1.Columns.AddVisible(fieldName: "Category_Name", caption: "Category Name");
//the col2 column is instantly visible and placed last
//col2.Visible returns "true"
//col2.VisibleIndex returns "gridView1.VisibleColumns.Count - 1"
Dim col1 As GridColumn = gridView1.Columns.Add()
'col1.Visible returns "false"
'col1.VisibleIndex returns "-1"
Dim col2 As GridColumn = gridView1.Columns.AddVisible(fieldName:= "Category_Name", caption:= "Category Name")
'the col2 column is instantly visible and placed last
'col2.Visible returns "true"
'col2.VisibleIndex returns "gridView1.VisibleColumns.Count - 1"
Grouped columns are considered as visible regardless of the GridOptionsView.ShowGroupedColumns setting.
gridView1.OptionsView.ShowGroupedColumns = false;
//colCategory.GroupIndex returns "-1"
//colCategory.Visible returns "true"
//colCategory.VisibleIndex returns "5"
colCategory.GroupIndex = 0;
//Grid data is now groupped by the "Category" column
//colCategory.Visible returns "true"
//colCategory.VisibleIndex returns "5"
gridView1.OptionsView.ShowGroupedColumns = False
'colCategory.GroupIndex returns "-1"
'colCategory.Visible returns "true"
'colCategory.VisibleIndex returns "5"
colCategory.GroupIndex = 0
'Grid data is now groupped by the "Category" column
'colCategory.Visible returns "true"
'colCategory.VisibleIndex returns "5"
For that reason, to iterate through all columns in the column header panel, use the VisibleColumns collection instead of the ColumnView.Columns collection.
If you decrease the VisibleIndex to move a column closer to the View’s left edge, the columns takes its new position as expected.
//the "Price" column is the 6th column in a View
//colPrice.VisibleIndex returns "5"
colPrice.VisibleIndex = 2;
//the "Price" column is now the 3rd column in a View
//colPrice.VisibleIndex = 2
'the "Price" column is the 6th column in a View
'colPrice.VisibleIndex returns "5"
colPrice.VisibleIndex = 2
'the "Price" column is now the 3rd column in a View
'colPrice.VisibleIndex = 2
If you increase the VisibleIndex to move a column to the View’s right edge, the column will be placed before the column that currently occupies the desired position.
//the "Model Name" column is the View's 1st column
//colModelName.VisibleIndex returns "0"
colModelName.VisibleIndex = 2;
//the "Model Name" column is now the 2nd column in a View
//colModelName.VisibleIndex returns "1"
'the "Model Name" column is the View's 1st column
'colModelName.VisibleIndex returns "0"
colModelName.VisibleIndex = 2
'the "Model Name" column is now the 2nd column in a View
'colModelName.VisibleIndex returns "1"
The ColumnView.ColumnPositionChanged event fires whenever a column changes its position.
The following code snippets (auto-collected from DevExpress Examples) contain references to the VisibleIndex 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.
GridColumn col1 = lookUpLabelProduct.Properties.View.Columns.AddField("Name");
col1.VisibleIndex = 0;
lookUpLabelProduct.Properties.DisplayMember = "Name";
winforms-grid-implement-serializable-column-property/CS/Q242361/Grid.cs#L71
public override int VisibleIndex {
get { return base.VisibleIndex; }
set {
buttonColumn.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
buttonColumn.VisibleIndex = 0;
buttonColumn.OptionsColumn.AllowEdit = true;
GridColumnInfoArgs args = viewInfo.ColumnsInfo[col];
if (col.VisibleIndex == 0)
HotTrackRectangle = new Rectangle(rowInfo.ButtonBounds.Right + 2, rowInfo.Bounds.Y,
winforms-grid-scrolling-by-columns/CS/WindowsApplication3/GridHScrollHelper.cs#L38
else {
if(firstColumnInfo.Column.VisibleIndex != 0)
SetLeftCoord(firstColumnInfo.Bounds.Width - firstColumnInfo.Bounds.Right + Indent, false);
Dim col1 As GridColumn = lookUpLabelProduct.Properties.View.Columns.AddField("Name")
col1.VisibleIndex = 0
lookUpLabelProduct.Properties.DisplayMember = "Name"
winforms-grid-visualize-master-detail-data/VB/Form1.vb#L42
'Hide the CategoryID column for the master View
gridView1.Columns("CategoryID").VisibleIndex = -1
'Present data in the Picture column as Images
winforms-grid-implement-serializable-column-property/VB/Q242361/Grid.vb#L108
Get
Return MyBase.VisibleIndex
End Get
Dim args As GridColumnInfoArgs = viewInfo.ColumnsInfo(col)
If col.VisibleIndex = 0 Then
HotTrackRectangle = New Rectangle(rowInfo.ButtonBounds.Right + 2, rowInfo.Bounds.Y, args.Bounds.Width + args.Bounds.X - rowInfo.ButtonBounds.Right - 2, rowInfo.Bounds.Height)
winforms-grid-scrolling-by-columns/VB/WindowsApplication3/GridHScrollHelper.vb#L37
Else
If firstColumnInfo.Column.VisibleIndex <> 0 Then
SetLeftCoord(firstColumnInfo.Bounds.Width - firstColumnInfo.Bounds.Right + Indent, False)
See Also