shawne
Well-known member
I'm trying to programmatically create a datagrid. I found the following code snippet, but I get a "Type expected." error when trying to declare the datagrid. I'm sure i'm missing something very simple, but I'm having a rough time figuring it out. Here is the snippet, thanks for any help:
Public Sub CreateGrid()
'declare a new datagrid and set properties
' Dim DataGrid1 As DataGrid
Dim DataGrid1 As New DataGrid
DataGrid1.BorderWidth = 2
DataGrid1.CellPadding = 10
DataGrid1.GridLines = Both
DataGrid1.BorderColor = Color.Blue
DataGrid1.ShowHeader = True
DataGrid1.AutoGenerateColumns = False
DataGrid1.SelectedItemStyle.BackColor = Color.Yellow
'add bound columns to the datagrid
Dim datagridcol As New BoundColumn
datagridcol.HeaderText = "Candy Type"
datagridcol.DataField = "CandyType"
DataGrid1.Columns.Add(datagridcol)
datagridcol = New BoundColumn
datagridcol.HeaderText = "Description"
datagridcol.DataField = "CandyDescription"
DataGrid1.Columns.Add(datagridcol)
Dim selectcol As New ButtonColumn
selectcol.ButtonType = ButtonColumnType.PushButton
selectcol.Text = "Purchase"
selectcol.CommandName = "Select"
DataGrid1.Columns.Add(selectcol)
'bind datagrid
DataGrid1.DataSource = GetDataSet()
DataGrid1.DataBind()
'add datagrid to the page
Page.Controls(1).Controls.Add(DataGrid1)
End Sub
Private Function GetDataSet()
Dim ws As New localhost.SweetsService
Dim ds As New DataSet
ds = ws.GetCandyInfo("truffles")
Return ds
End Function
End Class