Xceed Grid for WinForms supports populating the grid with both bound data (data retrieved from a data source) and unbound data (data provided manually) by allowing you add custom columns in addition to the automatically added data bound columns.
Basic steps
In order to populate the grid with both bound and unbound data, the following steps are required:
Demonstration
The following example demonstrates how to populate the grid using both bound and unbound data. The 2 unbound columns will contain the date of the week displayed as a string as well as the current time.
VB.NET |
Copy Code |
Imports Xceed.Grid
'Create a new instance of the GridControl class Dim grid As New GridControl()
' Bind the grid to a data source. For the purposes of this example ' we will assume that there is a dataset named dataSet11 that ' contains a table called Orders.
gridControl1.DataSource = dataSet11 gridControl1.DataMember = "Orders"
' We could have also used the SetDataBinding method. 'gridControl1.SetDataBinding( dataSet11, "Orders" )
' Subscribe to the AddingDataRow event AddHandler gridControl1.AddingDataRow, AddressOf Me.grid_AddingDataRow
' Add the first column to the grid. This column will contain the day ' of the week displayed as text.
Dim column1 As New Column( "Day", GetType( string ) ) gridControl1.Columns.Add( column1 )
' Create the second column that will display the hour. Dim column2 As New Column( "Time", GetType( DateTime ) )
' Since we only want the time, we will set the column's ' FormatSpecifier to a LongTimePattern column2.FormatSpecifier = "T"
' Add the second column to the grid. gridControl1.Columns.Add( column2 )
' This procedure will handle the AddingDataRow event and will ' provide the data for our 2 unbound columns. Private Sub grid_AddingDataRow( ByVal sender As Object, ByVal e As AddingDataRowEventArgs )
e.DataRow.Cells( "Day" ).Value = DateTime.Today.Date.DayOfWeek.ToString() e.DataRow.Cells( "Time" ).Value = DateTime.Now End Sub
|
C# |
Copy Code |
using Xceed.Grid;
// Add the first column to the grid. This column will contain the day // of the week displayed as text.
Column column1 = new Column( "Day", typeof( string ) ); gridControl1.Columns.Add( column1 );
// Create the second column that will display the hour. Column column2 = new Column( "Time", typeof( DateTime ) );
// Since we only want the time, we will set the column's // FormatSpecifier to a LongTimePattern
column2.FormatSpecifier = "T";
// Add the second column to the grid. gridControl1.Columns.Add( column2 );
// Subscribe to the AddingDataRow event gridControl1.AddingDataRow += new AddingDataRowEventHandler( this.grid_AddingDataRow );
// Bind the grid to a data source. For the purposes of this example // we will assume that there is a dataset named dataSet11 that // contains a table called Orders.
gridControl1.DataSource = dataSet11; gridControl1.DataMember = "Orders";
// We could have also used the SetDataBinding method. //gridControl1.SetDataBinding( dataSet11, "Orders" );
// This procedure will handle the AddingDataRow event and will // provide the data for our 2 unbound columns.
private void grid_AddingDataRow( object sender, AddingDataRowEventArgs e ) { try { e.DataRow.Cells[ "Day" ].Value = DateTime.Today.Date.DayOfWeek.ToString(); e.DataRow.Cells[ "Time" ].Value = DateTime.Now; } catch( Exception exception ) { MessageBox.Show( exception.ToString() ); } }
|