Listing 2: Code for Dynamically Generating a Data-Bound Form
Imports System.Configuration Imports System.Data Imports System.Data.SqlClient Imports System.Reflection Public Class Form2 Private Sub Form2_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim table As DataTable = Nothing Dim connectionString As String = _ My.Settings.northwndConnectionString Using connection As SqlConnection = _ New SqlConnection(connectionString) connection.Open() Dim command As SqlCommand = _ New SqlCommand("SELECT * FROM CUSTOMERS", connection) Dim adapter As SqlDataAdapter = New SqlDataAdapter(command) table = New DataTable() adapter.Fill(table) End Using GenerateForm(table) End Sub Private Sub GenerateForm(ByVal table As DataTable) Dim label As Label Dim textBox As TextBox Dim I As Integer For I = 0 To table.Columns.Count - 1 label = New Label() label.Location = New Point(10, I * 22 + 10) label.AutoSize = True label.Text = table.Columns(I).ColumnName + ":" Controls.Add(label) textBox = New TextBox textBox.Location = New Point(100, I * 22 + 10) textBox.Width = 200 textBox.DataBindings.Add("Text", _ table, table.Columns(I).ColumnName) Controls.Add(textBox) Next End Sub End Class
Tidak ada komentar:
Posting Komentar