Menu Horisontal

Senin, 23 April 2012

101 Ways to Manipulate the DataGridView Control (Bagian1)


ne of the common controls that you will usually use in Windows Forms programming is the DataGridView control. The DataGridView control is a new control in Windows Forms 2.0 and it replaces the DataGrid control in Windows Forms 1.0. It is a very powerful and versatile control that is suitable for displaying tabular data from various data sources. The DataGridView control is so flexible—exposing so many properties, methods, and events—that it can be quite intimidating for beginners.



The aim of this article is to get you jumpstarted with DataGridView control so that you can be productive right away. I have tried to cover most of the common tasks, but this is by no means exhaustive coverage of the capabilities of the DataGridView control.In this article, I assume that you have a Windows application project and have a DataGridView control contained within the default Form1, as shown in Figure 1.
Binding to an Array
You can bind an array to a DataGridView control. Consider the following code snippet:

Dim arr() As String = _
{"Product 1", "Product 24", "Product 356"}
DataGridView1.DataSource = arr
Here, arr() is a string array and contains three elements. It is bound to the DataGridView control using the DataSource property. Figure 2 shows the result of the data binding.

Figure 1. To get started, create a new Windows application project and put a DataGridView control on Form1.

Figure 2. The result of binding a string array to a DataGridView control is shown.



The result may be surprising to you, as you may expect the DataGridView control to display each individual element in the array. Instead, the length of each element is displayed. The reason behind this behavior is because the DataGridView control looks for the first public property of the object to which it is binding. In this case, the first public property of a string array is actually the length of each element contained within it. Hence the results above.To correctly bind a string array to a DataGridView control, you can wrap the string type with a class so that it exposes a public property that returns the content of each element, like this:

Public Class CStringItem
Private _str As String
Public Sub New(ByVal str As String)
_str = str
End Sub
Public Property Name() As String
Get
Return _str
End Get
Set(ByVal value As String)
_str = value
End Set
End Property
End Class
The following code snippet declares arr() to be an array of CStringItem type and initializes it with three items. It is then bound to the DataGridView control:

'---modified array---
Dim arr() As CStringItem = { _
New CStringItem("Product 1"), _
New CStringItem("Product 2"), _
New CStringItem("Product 3")}
DataGridView1.DataSource = arr
Figure 3 shows the output of the data binding. Notice that the public property Name is used as the header text for the column in the DataGridView control.

Figure 3. The output of binding an array of objects to a DataGridView control is shown.

Figure 4. The result of binding an array of CStudent objects to a DataGridView control is shown.

Binding to Custom Objects
The following example shows how you can bind a DataGridView control to a more complex object. In the CStudent class, you have two public properties: Name and ID.

Public Class CStudent
Private _name As String
Private _ID As String
Public Sub New(ByVal id As String, ByVal name As String)
_ID = id
_name = name
End Sub
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property ID() As String
Get
Return _ID
End Get
Set(ByVal value As String)
_ID = value
End Set
End Property
End Class
Data-binding the DataGridView control to an array of the CStudent objects yields the result shown in Figure 4.

'---binding to custom object---
Dim students() As CStudent = _
{New CStudent("123-456-789", "John"), _
New CStudent("456-123-789", "Mary")}
DataGridView1.DataSource = students

Tidak ada komentar: