Xceed Grid for WinForms v4.3 Documentation
InitializingDetailGrid event

Welcome to Xceed Grid for WinForms v4.3 > Basic Concepts > Events > InitializingDetailGrid event

The InitializingDetailGrid event is raised for each DataRow about to be added to the main grid or one of the detail grids to initialize its detail grid(s).

Basic steps - C#

To subscribe to the InitializingDetailGrid event, the following steps must be performed:

  • Obtain a reference to a GridControl or DetailGrid object. 

  • Subscribe to the InitializingDetailGrid event of the GridControl or DetailGrid object using the InitializingDetailGridEventHandler delegate class. 

  • Create a new method that will handle the events that are raised. 

  • Place the desired code in the newly created event handler.

Basic steps - VB.NET

To subscribe to the InitializingDetailGrid event, the following steps must be performed:

Demonstration

The following example demonstrates how to use the InitializingDetailGrid event to populate the contents of a detail grid according to the values of its parent DataRow.

VB.NET
Copy Code
Dim grid As New GridControl()

grid.BeginInit()           

Me.Controls.Add( grid )

grid.Dock = DockStyle.Fill     

grid.FixedHeaderRows.Add( New ColumnManagerRow() )

' Populate the main grid with data. For the purposes of this example

' we will only add 2 DataRows to the main grid.

grid.Columns.Add( New Column( "FirstName", GetType( string ) ) )

grid.Columns.Add( New Column( "LastName", GetType( string ) ) )

grid.Columns.Add( New Column( "FamilyID", GetType( integer ) ) )

grid.Columns.Add( New Column( "Occupation", GetType( string ) ) )     

Dim row1 As Xceed.Grid.DataRow = grid.DataRows.AddNew()

row1.Cells( "FirstName" ).Value = "Peter"

row1.Cells( "LastName" ).Value = "Griffin"

row1.Cells( "FamilyID" ).Value = 1

row1.Cells( "Occupation" ).Value = "Fisherman"

row1.EndEdit()

Dim row2 As Xceed.Grid.DataRow = grid.DataRows.AddNew()

row2.Cells( "FirstName" ).Value = "Homer"

row2.Cells( "LastName" ).Value = "Simpson"

row2.Cells( "FamilyID" ).Value = 2

row2.Cells( "Occupation" ).Value = "Nuclear Technician"

row2.EndEdit()      

' Create the detail grid that will contain our detail data.

Dim detail As New DetailGrid()

detail.HeaderRows.Add( New ColumnManagerRow() )

' Add the columns to our detail grid

detail.Columns.Add( New Column( "FirstName", GetType( string ) ) )

detail.Columns.Add( New Column( "Relationship", GetType( string ) ) )

detail.Columns.Add( New Column( "FamilyID", GetType( integer ) ) )

' Add our detail grid to the main grid's collection of DetailGridTemplates.

grid.DetailGridTemplates.Add( detail )

' Subscribe to the InitializingNewDetailGrid event so we can provide

' detail data to our detail grids according to the values of their

' parent DataRows.

AddHandler grid.InitializingDetailGrid, AddressOf Me.init_details      

grid.EndInit()

' Fill our detail grids with data!

Private Sub init_details( ByVal sender As object, ByVal e As InitializingDetailGridEventArgs )

  Select Case CType( e.Grid.ParentDataRow.Cells( "FamilyID" ).Value, Integer )

    Case 1:

      Dim row1 As Xceed.Grid.DataRow = e.Grid.DataRows.AddNew()

      row1.Cells( "FirstName" ).Value = "Lois"

      row1.Cells( "Relationship" ).Value = "Wife"

      row1.Cells( "FamilyID" ).Value = 1

      row1.EndEdit()     

      Dim row2 As Xceed.Grid.DataRow = e.Grid.DataRows.AddNew()

      row2.Cells( "FirstName" ).Value = "Meg"

      row2.Cells( "Relationship" ).Value = "Daughter"

      row2.Cells( "FamilyID" ).Value = 1

      row2.EndEdit()

      

      Dim row3 As Xceed.Grid.DataRow = e.Grid.DataRows.AddNew()

      row3.Cells( "FirstName" ).Value = "Chris"

      row3.Cells( "Relationship" ).Value = "Son"

      row3.Cells( "FamilyID" ).Value = 1

      row3.EndEdit()

 

      ' ...;

 

    Case 2:

      Dim row1 As Xceed.Grid.DataRow = e.Grid.DataRows.AddNew()

      row1.Cells( "FirstName" ).Value = "Marge"

      row1.Cells( "Relationship" ).Value = "Wife"

      row1.Cells( "FamilyID" ).Value = 2

      row1.EndEdit() 

      

      Dim row2 As Xceed.Grid.DataRow = e.Grid.DataRows.AddNew()

      row2.Cells( "FirstName" ).Value = "Bart"

      row2.Cells( "Relationship" ).Value = "Son"

      row2.Cells( "FamilyID" ).Value = 2

      row2.EndEdit()

 

      ' ...

   End Select

End Sub
C#
Copy Code
GridControl grid = new GridControl();

 

grid.BeginInit();

            

this.Controls.Add( grid );

grid.Dock = DockStyle.Fill;

      

grid.FixedHeaderRows.Add( new ColumnManagerRow() );

 

// Populate the main grid with data. For the purposes of this example

// we will only add 2 DataRows to the main grid.

grid.Columns.Add( new Column( "FirstName", typeof( string ) ) );

grid.Columns.Add( new Column( "LastName", typeof( string ) ) );

grid.Columns.Add( new Column( "FamilyID", typeof( int ) ) );

grid.Columns.Add( new Column( "Occupation", typeof( string ) ) );      

      

Xceed.Grid.DataRow row1 = grid.DataRows.AddNew();

row1.Cells[ "FirstName" ].Value = "Peter";      

row1.Cells[ "LastName" ].Value = "Griffin";

row1.Cells[ "FamilyID" ].Value = 1;

row1.Cells[ "Occupation" ].Value = "Fisherman";

row1.EndEdit();

 

Xceed.Grid.DataRow row2 = grid.DataRows.AddNew();

row2.Cells[ "FirstName" ].Value = "Homer";

row2.Cells[ "LastName" ].Value = "Simpson";

row2.Cells[ "FamilyID" ].Value = 2;

row2.Cells[ "Occupation" ].Value = "Nuclear Technician";

row2.EndEdit();      

 

// Create the detail grid that will contain our detail data.

DetailGrid detail = new DetailGrid();      

 

detail.HeaderRows.Add( new ColumnManagerRow() );

 

// Add the columns to our detail grid

detail.Columns.Add( new Column( "FirstName", typeof( string ) ) );

detail.Columns.Add( new Column( "Relationship", typeof( string ) ) );

detail.Columns.Add( new Column( "FamilyID", typeof( int ) ) );

 

// Add our detail grid to the main grid's collection of DetailGridTemplates.

grid.DetailGridTemplates.Add( detail );  

 

// Subscribe to the InitializingNewDetailGrid event so we can provide

// detail data to our detail grids according to the values of their

// parent DataRows.

grid.InitializingDetailGrid += new InitializingDetailGridEventHandler( this.init_details );

      

grid.EndInit();

 

// Fill our detail grids with data!

private void init_details( object sender, InitializingDetailGridEventArgs e )

{

  switch( ( int )e.Grid.ParentDataRow.Cells[ "FamilyID" ].Value )

  {

    case 1:

    {

      Xceed.Grid.DataRow row1 = e.Grid.DataRows.AddNew();

      row1.Cells[ "FirstName" ].Value = "Lois";

      row1.Cells[ "Relationship" ].Value = "Wife";

      row1.Cells[ "FamilyID" ].Value = 1;

      row1.EndEdit(); 

       

      Xceed.Grid.DataRow row2 = e.Grid.DataRows.AddNew();

      row2.Cells[ "FirstName" ].Value = "Meg";

      row2.Cells[ "Relationship" ].Value = "Daughter";

      row2.Cells[ "FamilyID" ].Value = 1;

      row2.EndEdit(); 

 

      Xceed.Grid.DataRow row3 = e.Grid.DataRows.AddNew();

      row3.Cells[ "FirstName" ].Value = "Chris";

      row3.Cells[ "Relationship" ].Value = "Son";

      row3.Cells[ "FamilyID" ].Value = 1;

      row3.EndEdit(); 

 

      // ...         

      break;

    }

    case 2:

    {

      Xceed.Grid.DataRow row1 = e.Grid.DataRows.AddNew();

      row1.Cells[ "FirstName" ].Value = "Marge";

      row1.Cells[ "Relationship" ].Value = "Wife";

      row1.Cells[ "FamilyID" ].Value = 2;

      row1.EndEdit(); 

      

      Xceed.Grid.DataRow row2 = e.Grid.DataRows.AddNew();

      row2.Cells[ "FirstName" ].Value = "Bart";

      row2.Cells[ "Relationship" ].Value = "Son";

      row2.Cells[ "FamilyID" ].Value = 2;

      row2.EndEdit(); 

 

      // ...

      break;

    }

  }

}