Back to Devexpress

GridControl.ForceInitialize() Method

windowsforms-devexpress-dot-xtragrid-dot-gridcontrol-3a139eb7.md

latest7.0 KB
Original Source

GridControl.ForceInitialize() Method

Forces the grid control to finish its initialization. Call this method when the form is loading, before you start to change the grid’s options.

Namespace : DevExpress.XtraGrid

Assembly : DevExpress.XtraGrid.v25.2.dll

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

Declaration

csharp
public virtual void ForceInitialize()
vb
Public Overridable Sub ForceInitialize

Remarks

After the grid control has been initialized, you can manipulate its settings safely. Call the ForceInitialize method before you:

  • group and/or sort data;
  • change summary settings;
  • expand and collapse rows;
  • create columns and change their visibility;
  • restore a layout, etc.

Note

The ForceInitialize method has no effect until the form’s Load event occurs. For instance, nothing will happen if you add this method in the form’s constructor.

Note

If you call one of the Move methods (e.g., MoveBy(Int32), MoveFirst(), MoveNext(), etc.) and no data source is assigned to the grid control, an exception is thrown. Call the ForceInitialize method to avoid InvalidCastException.

Example

The code sample below illustrates how to safely add a column to a grid.

csharp
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;

private void XtraForm1_Load(object sender, EventArgs e)
{
    gridControl1.ForceInitialize();
    gridView1.Columns.Add();
    gridView1.Columns[gridView1.Columns.Count - 1].Caption = "Actions";
    gridView1.Columns[gridView1.Columns.Count - 1].VisibleIndex = gridView1.Columns.Count;
}
vb
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Grid

Private Sub XtraForm1_Load(ByVal sender As Object, ByVal e As EventArgs)
    gridControl1.ForceInitialize()
    gridView1.Columns.Add()
    gridView1.Columns(gridView1.Columns.Count - 1).Caption = "Actions"
    gridView1.Columns(gridView1.Columns.Count - 1).VisibleIndex = gridView1.Columns.Count
End Sub

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

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-grid-use-layoutview-as-master-view/CS/WindowsApplication3/Form1.cs#L27

csharp
gridControl1.ForceInitialize();
helper = new MasterDetailHelper(layoutView1, ViewType.Grid);

winforms-preserve-grid-state-on-refresh/CS/Form1.cs#L324

csharp
gridControl1.DataSource = dataSet11.Tables["Customers"];
    gridControl1.ForceInitialize();
}

winforms-grid-vertically-align-detail-views/CS/DetailsUnderEachOther/Form1.cs#L33

csharp
private void DemoExpand() {
    gridControl1.ForceInitialize();
    gvMaster.SetMasterRowExpanded(0, true);

how-to-export-cell-range-to-a-datatable/CS/ExportToDataTableExample/Form1.cs#L200

csharp
newForm.Controls.Add(grid);
grid.ForceInitialize();
((DevExpress.XtraGrid.Views.Grid.GridView)grid.FocusedView).OptionsView.ShowGroupPanel = false;

XPO_how-to-create-an-xpclassinfo-descendant-to-dynamically-build-a-persistent-class-structure-e1729/CS/DynamicDataTable/Form1.cs#L50

csharp
//grdTableBrowser.ServerMode = true;
    grdTableBrowser.ForceInitialize();
} catch (Exception ex) {

winforms-grid-use-layoutview-as-master-view/VB/WindowsApplication3/Form1.vb#L28

vb
gridControl1.ForceInitialize()
helper = New MasterDetailHelper(layoutView1, ViewType.Grid)

winforms-grid-visualize-master-detail-data/VB/Form1.vb#L35

vb
gridControl1.DataSource = dataSet11.Tables("Categories")
gridControl1.ForceInitialize()
'Assign a CardView to the relationship

winforms-preserve-grid-state-on-refresh/VB/Form1.vb#L317

vb
gridControl1.DataSource = dataSet11.Tables("Customers")
    gridControl1.ForceInitialize()
End Sub

winforms-grid-vertically-align-detail-views/VB/DetailsUnderEachOther/Form1.vb#L35

vb
Private Sub DemoExpand()
    gridControl1.ForceInitialize()
    gvMaster.SetMasterRowExpanded(0, True)

how-to-export-cell-range-to-a-datatable/VB/ExportToDataTableExample/Form1.vb#L186

vb
newForm.Controls.Add(grid)
grid.ForceInitialize()
CType(grid.FocusedView, DevExpress.XtraGrid.Views.Grid.GridView).OptionsView.ShowGroupPanel = False

See Also

GridControl Class

GridControl Members

DevExpress.XtraGrid Namespace