windowsforms-devexpress-dot-xtragrid-dot-gridcontrol-3a139eb7.md
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
public virtual void ForceInitialize()
Public Overridable Sub ForceInitialize
After the grid control has been initialized, you can manipulate its settings safely. Call the ForceInitialize method before you:
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.
The code sample below illustrates how to safely add a column to a grid.
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;
}
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
gridControl1.ForceInitialize();
helper = new MasterDetailHelper(layoutView1, ViewType.Grid);
winforms-preserve-grid-state-on-refresh/CS/Form1.cs#L324
gridControl1.DataSource = dataSet11.Tables["Customers"];
gridControl1.ForceInitialize();
}
winforms-grid-vertically-align-detail-views/CS/DetailsUnderEachOther/Form1.cs#L33
private void DemoExpand() {
gridControl1.ForceInitialize();
gvMaster.SetMasterRowExpanded(0, true);
how-to-export-cell-range-to-a-datatable/CS/ExportToDataTableExample/Form1.cs#L200
newForm.Controls.Add(grid);
grid.ForceInitialize();
((DevExpress.XtraGrid.Views.Grid.GridView)grid.FocusedView).OptionsView.ShowGroupPanel = false;
//grdTableBrowser.ServerMode = true;
grdTableBrowser.ForceInitialize();
} catch (Exception ex) {
winforms-grid-use-layoutview-as-master-view/VB/WindowsApplication3/Form1.vb#L28
gridControl1.ForceInitialize()
helper = New MasterDetailHelper(layoutView1, ViewType.Grid)
winforms-grid-visualize-master-detail-data/VB/Form1.vb#L35
gridControl1.DataSource = dataSet11.Tables("Categories")
gridControl1.ForceInitialize()
'Assign a CardView to the relationship
winforms-preserve-grid-state-on-refresh/VB/Form1.vb#L317
gridControl1.DataSource = dataSet11.Tables("Customers")
gridControl1.ForceInitialize()
End Sub
winforms-grid-vertically-align-detail-views/VB/DetailsUnderEachOther/Form1.vb#L35
Private Sub DemoExpand()
gridControl1.ForceInitialize()
gvMaster.SetMasterRowExpanded(0, True)
how-to-export-cell-range-to-a-datatable/VB/ExportToDataTableExample/Form1.vb#L186
newForm.Controls.Add(grid)
grid.ForceInitialize()
CType(grid.FocusedView, DevExpress.XtraGrid.Views.Grid.GridView).OptionsView.ShowGroupPanel = False
See Also