A grid control that automatically updates its data.
Implement the GetColumns event to return column definitions. Then implement the GetItems event to return objects to populate the rows. Implement the GetCellValue and SetCellValue events to populate the cells and respond to edits. Finally, implement RowAdded and RowDeleted to create and delete objects when the user adds and deletes rows.
An update grid control has very few events. The first that you want to implement is GetColumns. You need to return a ColumnDefinitions object. Add columns using dot-chaining:
Then, implement the GetItems event to return the collection of objects. These are objects of your own type, not of some special "row" type. They don't even have to implement a special interface.
Private Function itemsGrid_GetColumns() As UpdateControls.Forms.ColumnDefinitions
Return New UpdateControls.Forms.ColumnDefinitions() _
.Add("Name", GetType(String)) _
.Add("Price", GetType(Decimal)) _
.Add("Quantity", GetType(Integer)) _
.AddReadOnly("Total", GetType(Decimal))
End Function
Fill the grid and respond to changes by implementing GetCellValue and SetCellValue.
Private Function itemsGrid_GetItems() As System.Collections.IEnumerable
Return _order.Items
End Function
Finally, to allow the user to add and delete rows, implement the RowAdded and RowDeleted events.
Private Function itemsGrid_GetCellValue(ByVal tag As Object, ByVal column As Integer) As Object
Dim item As Item = DirectCast(tag, Item)
If column = 0 Then
Return item.Name
ElseIf column = 1 Then
Return item.Price
ElseIf column = 2 Then
Return item.Quantity
ElseIf column = 3 Then
Return item.Total
Else
Return Nothing
End If
End Function
Private Sub itemsGrid_SetCellValue(ByVal tag As Object, ByVal column As Integer, ByVal value As Object)
Dim item As Item = DirectCast(tag, Item)
If column = 0 Then
item.Name = DirectCast(value, String)
ElseIf column = 1 Then
item.Price = CDec(value)
ElseIf column = 2 Then
item.Quantity = CInt(value)
End If
End Sub
That’s all it takes. The UpdateGrid is actually a pretty simple control. Just don’t over use it.
Private Function itemsGrid_RowAdded() As Object
Return _order.NewItem()
End Function
Private Sub itemsGrid_RowDeleted(ByVal tag As Object)
_order.DeleteItem(DirectCast(tag, Item))
End Sub
| New | () |
| DataSource | As Object |
|
| Enabled | As Boolean | True if the control is enabled (read-only). |
| SelectedItems | As System.Collections.ICollection | The collection of items representing the selected rows. |
| GetCellValue | (ByVal tag As Object, ByVal column As Integer) | As Object | Event fired to get the value of a cell. |
| GetColumns | () | As ColumnDefinitions | Event fired to get the column definitions. |
| GetEnabled | () | As Boolean | Event fired to determine whether the control is enabled. |
| GetItems | () | As System.Collections.IEnumerable | Event fired to get the list of items. |
| RowAdded | () | As Object | Event fired when the user adds a row. |
| RowDeleted | (ByVal tag As Object) | Event fired when the user deletes a row. | |
| SetCellValue | (ByVal tag As Object, ByVal column As Integer, ByVal value As Object) | Event fired when the user edits the value of a cell. |