Data can be provided manually to the grid by adding new DataRow objects to the grid's collection of DataRows. This allows you to populate the grid with data without binding it to a data source. For a more "real-life" example, please refer to the UnboundTreeView sample application.
Basic steps
In order to provide data manually to the grid, the following steps are required:
-
Create a new instance of the GridControl class or use an existing instance.
-
Add the desired number of unbound columns to the grid
-
Subscribe to the AddingDataRow event or fill the data on the new row returned by the AddNew method.
-
Add the desired number of DataRow objects to the grid's collection of data rows using the collections AddNew method.
Make sure that you call the EndEdit method after calling AddNew in order to add the newly created datarow to the grid. Note that if EndEdit is not called and another call to AddNew is made, the previous row created with AddNew will be added to the grid.
If DataRows are added to the grid while the grid is not bound to a data source and then a data source is assigned to the grid, all the DataRows that were added to the grid will be removed.
Demonstration
The following example demonstrates how to provide data manually to the grid:
VB.NET |
Copy Code |
Imports Xceed.Grid
' Create a new instance of the GridControl class Dim grid As New GridControl()
' Add the grid to the form Me.Controls.Add( grid )
' Add the desired number of columns to the grid. ' In the case, we will only add 4
grid.Columns.Add( New Column( "column1", GetType( Integer ) ) ) grid.Columns.Add( New Column( "column2", GetType( Integer ) ) ) grid.Columns.Add( New Column( "column3", GetType( Integer ) ) ) grid.Columns.Add( New Column( "column4", GetType( Integer ) ) )
' Subscribe to the AddingDataRow event AddHandler grid.AddingDataRow, AddressOf Me.grid_AddingDataRow
' Add the desired number of DataRow objects to the grid's collection of datarows
Dim i As Integer For i = 0 To 50 grid.DataRows.AddNew()
' AddNew will return a reference to a new DataRow object whose ' cell values we can fill immediately, for the purposes of this ' example we will use the AddingDataRow event instead. ' ' Xceed.Grid.DataRow row = grid.DataRows.AddNew() ' row.Cells( 0 ).Value = 26 ' row.Cells( 0 ).Value = 27 ' row.Cells( 0 ).Value = 28 ' row.Cells( 0 ).Value = 29 ' row.EndEdit() ' etc. Next
' Private member variables that will be used to provide data to the grid. private m_random As new Random()
' This is the procedure that will handle the AddingDataRow event. Private Sub grid_AddingDataRow( ByVal sender As Object, ByVal e As AddingDataRowEventArgs )
Dim cell As Cell
Try For Each( cell in e.DataRow.Cells ) cell.Value = m_random.Next( 0, 5000 ) Next cell Catch exception As Exception MessageBox.Show( exception.ToString() ) End Try End Sub
|
C# |
Copy Code |
using Xceed.Grid;
// Add the desired number of columns to the grid. In the case, we will only add 4 gridControl1.Columns.Add( new Column( "column1", typeof( int ) ) ); gridControl1.Columns.Add( new Column( "column2", typeof( int ) ) ); gridControl1.Columns.Add( new Column( "column3", typeof( int ) ) ); gridControl1.Columns.Add( new Column( "column4", typeof( int ) ) );
// Subscribe to the AddingDataRow event gridControl1.AddingDataRow += new AddingDataRowEventHandler( grid_AddingDataRow );
// Add the desired number of DataRow objects to the grid's collection of datarows for( int i = 0; i < 50; i++ ) { gridControl1.DataRows.AddNew().EndEdit();
// AddNew will return a reference to a new DataRow object whose // cell values we can fill immediately, for the purposes of this // example we will use the AddingDataRow event instead. // // Xceed.Grid.DataRow row = gridControl1.DataRows.AddNew(); // row.Cells[ 0 ].Value = 25; // row.Cells[ 1 ].Value = 26; // row.Cells[ 2 ].Value = 27; // row.Cells[ 3 ].Value = 28; // row.EndEdit(); // etc. }
// Private member variables that will be used to provide data to the grid. private Random m_random = new Random();
// This is the procedure that will handle the AddingDataRow event. private void grid_AddingDataRow( object sender, AddingDataRowEventArgs e ) { try { foreach( Cell cell in e.DataRow.Cells ) { cell.Value = m_random.Next( 0, 5000 ); } } catch( Exception exception ) { MessageBox.Show( exception.ToString() ); } }
|
For a more "real-life" example, please refer to the UnboundGrid sample application.