Home Forums WinForms controls Xceed Grid for WinForms Add sub row Reply To: Add sub row

User (Old forums)
Member
Post count: 23064

You have no choice but to use a master/detail scenario for this.

If your data is not in a relational DB, you will need to feed the data manually to the grid.

The detail grids can be configured so they have only one column, with the header row hidden, with no spacing, so it appears as you described.  Here is an example of what you need to do if you do every thing in code (in the designer it would be a bit different because the grid creates a whole bunch of things automatically when it is dropped on the form) :

private void Form1_Load( object sender, EventArgs e )

{

    //Master grid 

    GridControl grid = new GridControl();

    this.Controls.Add( grid );

    grid.Dock = DockStyle.Fill;

    grid.InitializingDetailGrid += new InitializingDetailGridEventHandler( grid_InitializingDetailGrid );

    ColumnManagerRow headerRow = new ColumnManagerRow();

    grid.FixedHeaderRows.Add( headerRow );

    Column col = new Column( “Column 1”, typeof( string ) );

    grid.Columns.Add( col );

 

    //Detail grid

    DetailGrid detailGrid = new DetailGrid();

    detailGrid.TopMargin.Height = 1;

    detailGrid.BottomMargin.Height = 0;

    Column detailCol = new Column( “Column 1”, typeof( string ) );

    detailGrid.Columns.Add( detailCol );

    grid.DetailGridTemplates.Add( detailGrid );

    grid.UpdateDetailGrids();

 

    //Add 2 master rows

    for( int i = 0; i < 2; i++ )

    {

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

        row.Cells[ 0 ].Value = “Item “ + i.ToString();

        row.EndEdit();

    }

}

void grid_InitializingDetailGrid( object sender, InitializingDetailGridEventArgs e )

{

    //Add rows to the detail girds depending on the master row 

    if( e.Grid.ParentDataRow.Cells[ 0 ].Value.ToString() == “Item 0” )

    {

        for( int i = 1; i < 3; i++ )

        {

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

            row.Cells[ 0 ].Value = “sub item “ + i.ToString();

            row.EndEdit();

        }

    }

    else

    {

        for( int i = 3; i < 6; i++ )

        {

            Xceed.Grid.

DataRow row = e.Grid.DataRows.AddNew();

            row.Cells[ 0 ].Value =

“sub item “ + i.ToString();

            row.EndEdit();

        }

    }

}

 

Imported from legacy forums. Posted by André (had 624 views)