Customizing the DataGrid
Let’s start with a DataGrid that binds to a DataSet that is returned from a Web Service using the following code.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim ws As New localhost.SweetsService
Dim ds As New DataSet
ds = ws.GetCandyInfo("truffles")
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub
When the page loads, the DataSet is returned and bound to the DataGrid and appears like the following.
For this example, we want to add a custom checkbox control to this DataGrid control. The following steps show one of several ways that is can be done.
1. Right click on the DataGrid and select Property Builder.
2. Within the Property Builder add a “Template Column” and select “OK”
3. Right click on the DataGrid and select “Edit Template”. This menu item is now available after the “Template Column” was added. This will allow you to edit Columns[0] which is the custom item that we added.
4. The template editor has four sections: Header Template, Item Template, EditItem Template and Footer Template. Drag a checkbox control from the Tools menu into the Item Template section.
When you run the page the checkbox now appears in the DataGrid.