Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Someone recently asked me how you can programmatically create a DataGrid. So here is an example that I put together.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CreateGrid()
End Sub
Public Sub CreateGrid()
'declare a new datagrid and set properties
DataGrid1.BorderWidth = Unit.Pixel(2)
DataGrid1.CellPadding = 10
DataGrid1.GridLines = 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