Tips and tricks for .NET using ASP and VB code.

Bind a DropDownList to a code generated table.

Public Class LinqTest1
Inherits System.Web.UI.Page
Dim WithEvents LinqDataSource1 As New LinqDataSource

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
With LinqDataSource1
.ID = "LinqDataSource1"
End With
form1.Controls.Add(LinqDataSource1)
Dim DropDownList1 As New DropDownList
With DropDownList1
.ID = "DropDownList1"
.DataSourceID = "LinqDataSource1"
.DataTextField = "String Column"
.DataValueField = "Record ID Column"
End With
form1.Controls.Add(DropDownList1)
End Sub

Private Sub LinqDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles LinqDataSource1.Selecting
Dim DataTable1 As New Data.DataTable
While DataTable1.Columns.Contains("Record ID Column") = False
Dim NewDataColumn As New Data.DataColumn
NewDataColumn.ColumnName = "Record ID Column"
NewDataColumn.DataType = System.Type.GetType("System.Int64")
NewDataColumn.AutoIncrement = True
NewDataColumn.AutoIncrementSeed = 0
NewDataColumn.AutoIncrementStep = 1
DataTable1.Columns.Add(NewDataColumn)
Dim DataColumnArray() As Data.DataColumn = {DataTable1.Columns("Record ID Column")}
DataTable1.PrimaryKey = DataColumnArray
End While
While DataTable1.Columns.Contains("String Column") = False
Dim NewDataColumn As New Data.DataColumn
NewDataColumn.ColumnName = "String Column"
NewDataColumn.DataType = System.Type.GetType("System.String")
DataTable1.Columns.Add(NewDataColumn)
End While
Do 'Make NewDataRow variable name reusable by putting it in a loop.
Dim NewDataRow As Data.DataRow = DataTable1.NewRow
NewDataRow.Item("String Column") = "Text 1"
DataTable1.Rows.Add(NewDataRow)
Exit Do
Loop
Do
Dim NewDataRow As Data.DataRow = DataTable1.NewRow
NewDataRow.Item("String Column") = "Text 2"
DataTable1.Rows.Add(NewDataRow)
Exit Do
Loop
e.Result = DataTable1.DefaultView
End Sub

End Class

No comments:

Post a Comment

Search This Blog