The DataGrid control is a data-bound control that automatically displays a list of information in a series of rows and columns , as in a spreadsheet. A data-bound control is self-populating; that is, it fills itself with data when told the source of the data. A DataGrid control must be bound to a data source using the DataSource and DataMember properties at design time or the SetDataBinding method at run time. Typically, a DataGrid control is bound to a DataTable , DataView or DataSet class (discussed in Chapter 13). However, you can also bind a DataGrid control to any class that implements the IList interface. The ArrayList class, which we discussed in Chapter 6, implements the IList interface. In the GridDemo example, we will build an array of Item objects. These are the elements that will be displayed in our data grid. Our Item class, which represents a barcoded object in a store, is shown below: Public Class Item Private m_barCode As String Private m_description As String Private m_price As Decimal Public Sub New() End Sub Public Sub New(ByVal barCode As String, _ ByVal description As String, _ ByVal price As Decimal) m_barCode = barCode m_description = description m_price = price End Sub Public Property BarCode() As String ' code not shown End Property Public Property Description() As String ' code not shown End Property Public Property Price() As Decimal ' code not shown End Property Public Overrides Function ToString() As String Dim s As String s = String.Format(_ "{0} (Barcode: {1}) priced at {2:c2}", _ m_description, m_barCode, m_price) Return s End Function End Class When a DataGrid control is bound to a Dataset or DataTable , the columns of the underlying table are displayed in the grid. When the grid is bound to an object that implements the IList interface, the public properties of the elements in the underlying structure or class will be displayed. For example, in GridDemo the grid will display all the public properties of the Item objects. Figure 8-22 illustrates the GridDemo application. Figure 8-22. Using the DataGrid control. To achieve the effects shown in Figure 8-22, the DataGrid control was placed on the form and named dgItems . The form class then created an ArrayList containing Item objects. Finally, the DataSource property of the DataGrid was set to the ArrayList . Public Class MainForm Inherits System.Windows.Forms.Form Public itemList As New ArrayList() Public Sub New() ... itemList.Add(New Item(_ "1010101", "Grape Jelly", 2.49)) itemList.Add(New Item(_ "2010102", "Peanut Butter", 3.89)) itemList.Add(New Item(_ "3030303", "White Bread", 1.49)) dgItems.DataSource = itemList dgItems.ColumnHeadersVisible = True End Sub ... End Class Any changes made to the data in the data grid are immediately reflected back in the underlying objects found in the ArrayList . To illustrate this, we changed the price of peanut butter and then pressed the "Display itemList" button. Code in the Click event handler for this button is shown below: Private Sub btnDisplay_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) _ Handles btnDisplay.Click Dim s As String Dim it As Item For Each it In itemList s = s & it.ToString() & _ Microsoft.VisualBasic.ControlChars.CrLf Next MessageBox.Show(s, "Current Items", _ MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub The result of pressing this button is shown in Figure 8-23. You can see that the data in the underlying ArrayList has been modified. Figure 8-23. Changes to data in the DataGrid are reflected in the underlying ArrayList. |