VB.NET Combobox Tutorial datasource bind to datatable
A sample created on vb.net winform for tutorial binding to datatable getting the value and text
Public Class Form1 Sub New() ' This call is required by the designer. InitializeComponent() Me.ComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList Me.ComboBox1.FormattingEnabled = True Me.ComboBox1.Location = New System.Drawing.Point(12, 34) Me.ComboBox1.Name = "ComboBox1" Me.ComboBox1.Size = New System.Drawing.Size(231, 21) Me.ComboBox1.TabIndex = 0 ' Add any initialization after the InitializeComponent() call. 'Button1 ' Me.Button1.Location = New System.Drawing.Point(87, 106) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(75, 23) Me.Button1.TabIndex = 1 Me.Button1.Text = "Button1" Me.Button1.UseVisualStyleBackColor = True ' 'Label1 ' Me.Label1.AutoSize = True Me.Label1.Location = New System.Drawing.Point(12, 69) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(39, 13) Me.Label1.TabIndex = 2 Me.Label1.Text = "Label1" ' 'Label2 ' Me.Label2.AutoSize = True Me.Label2.Location = New System.Drawing.Point(94, 69) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(39, 13) Me.Label2.TabIndex = 3 Me.Label2.Text = "Label2" ' 'Button2 ' Me.Button2.Location = New System.Drawing.Point(35, 146) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(75, 23) Me.Button2.TabIndex = 4 Me.Button2.Text = "select1" Me.Button2.UseVisualStyleBackColor = True ' 'Button3 ' Me.Button3.Location = New System.Drawing.Point(129, 146) Me.Button3.Name = "Button3" Me.Button3.Size = New System.Drawing.Size(75, 23) Me.Button3.TabIndex = 5 Me.Button3.Text = "select 3" Me.Button3.UseVisualStyleBackColor = True ' 'Button4 ' Me.Button4.Location = New System.Drawing.Point(87, 187) Me.Button4.Name = "Button4" Me.Button4.Size = New System.Drawing.Size(75, 23) Me.Button4.TabIndex = 6 Me.Button4.Text = "select 5" Me.Button4.UseVisualStyleBackColor = True ' 'Form1 ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(262, 234) Me.Controls.Add(Me.Button4) Me.Controls.Add(Me.Button3) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.ComboBox1) Me.Name = "Form1" Me.Text = "Form1" End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim dt As New DataTable() Dim r As DataRow dt.Columns.Add("id", GetType(Integer)) dt.Columns.Add("name", GetType(String)) r = dt.NewRow r("id") = 1 r("name") = "Test" dt.Rows.Add(r) r = dt.NewRow r("id") = 3 r("name") = "Test2" dt.Rows.Add(r) r = dt.NewRow r("id") = 5 r("name") = "Test4" dt.Rows.Add(r) ComboBox1.DataSource = dt ComboBox1.DisplayMember = "name" ComboBox1.ValueMember = "id" End Sub '''''' When selection is changed ''' ''' ''' Private Sub ComboBox1_TabIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged Label1.Text = ComboBox1.SelectedValue.ToString() Label2.Text = ComboBox1.Text End Sub '''''' When Button is clicked ''' ''' ''' Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click ComboBox1.SelectedValue = 1 ComboBox1.Enabled = True Label1.Text = ComboBox1.SelectedValue Label2.Text = ComboBox1.Text End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click ComboBox1.SelectedValue = 3 ComboBox1.Enabled = False Label1.Text = ComboBox1.SelectedValue Label2.Text = ComboBox1.Text End Sub Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click ComboBox1.SelectedValue = 5 Label1.Text = ComboBox1.SelectedValue Label2.Text = ComboBox1.Text End Sub End Class
Comments
Post a Comment