Displays ADO+ data in a scrollable grid.
Object
MarshalByRefObject
MarshalByRefComponent
Control
RichControl
DataGrid
[Visual Basic] Public Class DataGrid Inherits RichControl Implements ISupportInitialize, IDataGridEditingService [C#] public class DataGrid : RichControl, ISupportInitialize, IDataGridEditingService [C++] public __gc class DataGrid : public RichControl, ISupportInitialize, IDataGridEditingService [JScript] public class DataGrid extends RichControl, ISupportInitialize, IDataGridEditingService
The DataGrid displays web-like links to child tables. You can click on a link to navigate to the child table. The back button is displayed to the left of the cursor to navigate back to any parent. The data from the parent rows is displayed below the caption and above the column headers. You can hide the parent rows by clicking the button to the right of the caption.
To use the DataGrid, set the DataSource property to a valid data source. Valid sources include the following:
To determine which cell is selected, use the CurrentCell property.
Change the value of any cell use the Item property (DataGrid indexer) which can take either the row and column index of the cell, or a DataGridCell.
You can monitor the OnCurrentCellChange event to detect when the user selects another cell. To determine which part of the control the user clicked, use the HitTest method in the OnClick. The HitTest method returns a DataGrid.HitTestInfo object which contains the row and column of a clicked area.
To manage the appearance of the control at run time, several properties for setting the color and caption attributes are available, including the CaptionForeColor, CaptionBackColor, SelectionForeColor, and so on.
The DataGrid can display any DataGridTable associated with the grid through the GridTablesCollection, which is accessed through the GridTables property.
Namespace: System.WinForms
Assembly: System.WinForms.dll
The following example creates several columns in a DataTable, fills each column with data, and sets a DataView component's DataTable property to the resulting object. A DataGrid control is then bound to the DataTable to display the data.
[Visual Basic]
' Create a table and display it in the DataGrid. Public Sub MakeDataTable() Dim myTable As DataTable Dim myCol As DataColumn Dim myRow As DataRow ' Create a DataTable myTable = New DataTable("MyTable") ' Add a new DataColumn set its DataType to string. myCol = New DataColumn("StringCol",Type.GetType("System.String")) ' Add the column to the DataTable. myTable.Columns.Add(myCol) ' Create six more columns of various types and add each to the table. myCol = New DataColumn("ByteCol", System.Type.GetType("System.Byte")) myTable.Columns.Add(myCol) myCol = New DataColumn("Int32Col", System.Type.GetType("System.Int16")) myTable.Columns.Add(myCol) myCol = New DataColumn("BooleanCol", System.Type.GetType("System.Boolean")) myTable.Columns.Add(myCol) myCol = New DataColumn("TimeSpanCol", System.Type.GetType("System.TimeSpan")) myTable.Columns.Add(myCol) myCol = New DataColumn("DateTimeCol", System.Type.GetType("System.DateTime")) myTable.Columns.Add(myCol) myCol = New DataColumn("CurrencyCol", System.Type.GetType("System.Currency")) myTable.Columns.Add(myCol) ' Populate the rows with values Dim i As Integer Dim c As Integer ' Create 10 records. For i = 0 To 9 ' Create a new DataRow using the NewRow method. myRow = myTable.NewRow ' Add the row. myTable.Rows.Add myRow Next Dim dv As DataView dv = New DataView(myTable) ' Bind DataGrid to DataView DataGrid1.DataSource = dv DataGrid1.PopulateColumns End Sub
DataGrid Members | System.WinForms Namespace | DataColumn | GridColumnsCollection | DataSet | ColumnsCollection | DataView | DataGridCell | DataGridColumn | DataRow | DataTable | DataRelation | NewRow