Back to Devexpress

GridColumn.VisibleIndex Property

windowsforms-devexpress-dot-xtragrid-dot-columns-dot-gridcolumn-3a11b1af.md

latest12.3 KB
Original Source

GridColumn.VisibleIndex Property

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

Declaration

csharp
[DefaultValue(-1)]
[DXCategory("Appearance")]
[XtraSerializableProperty]
[XtraSerializablePropertyId(3)]
public virtual int VisibleIndex { get; set; }
vb
<DXCategory("Appearance")>
<DefaultValue(-1)>
<XtraSerializableProperty>
<XtraSerializablePropertyId(3)>
Public Overridable Property VisibleIndex As Integer

Property Value

TypeDefaultDescription
Int32-1

A column’s position within the View. -1 for hidden columns.

|

Remarks

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.

csharp
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"
vb
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"

VisibleIndex for new columns

Columns added by the GridColumnCollection.Add method are initially hidden. To add a column and show it immediatelly, call the GridColumnCollection.AddVisible method instead.

csharp
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"
vb
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"

VisibleIndex for grouped columns

Grouped columns are considered as visible regardless of the GridOptionsView.ShowGroupedColumns setting.

csharp
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"
vb
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.

Change VisibleIndex to move columns

If you decrease the VisibleIndex to move a column closer to the View’s left edge, the columns takes its new position as expected.

csharp
//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
vb
'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.

csharp
//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"
vb
'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.

reporting-winforms-label-report-in-code/CS/Reporting_how-to-create-a-label-report-at-runtime-t473792/Form1.cs#L62

csharp
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

csharp
public override int VisibleIndex {
    get { return base.VisibleIndex; }
    set {

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

csharp
buttonColumn.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
buttonColumn.VisibleIndex = 0;
buttonColumn.OptionsColumn.AllowEdit = true;

winforms-grid-enable-editing-in-group-row-to-change-cell-values/CS/WindowsApplication3/GroupEditProvider.cs#L100

csharp
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

csharp
else {
    if(firstColumnInfo.Column.VisibleIndex != 0)
        SetLeftCoord(firstColumnInfo.Bounds.Width - firstColumnInfo.Bounds.Right + Indent, false);

reporting-winforms-label-report-in-code/VB/Reporting_how-to-create-a-label-report-at-runtime-t473792/Form1.vb#L54

vb
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

vb
'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

vb
Get
    Return MyBase.VisibleIndex
End Get

winforms-grid-enable-editing-in-group-row-to-change-cell-values/VB/WindowsApplication3/GroupEditProvider.vb#L99

vb
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

vb
Else
    If firstColumnInfo.Column.VisibleIndex <> 0 Then
        SetLeftCoord(firstColumnInfo.Bounds.Width - firstColumnInfo.Bounds.Right + Indent, False)

See Also

ShowGroupedColumns

GridColumnCollection.AddVisible*

AbsoluteIndex

Columns

IndexOf(GridColumn)

VisibleColumns

ColumnPositionChanged

GridColumn Class

GridColumn Members

DevExpress.XtraGrid.Columns Namespace