如何从组合框选择填写文本框。请更正我的代码 [英] How Text Boxes to be fill from combo box selection.Please Correct My Code
本文介绍了如何从组合框选择填写文本框。请更正我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好
我有一个组合框,其中包含来自Access DB.And两个文本框的员工姓名列表。当我从组合中选择一个名字时,我希望这样框,第一个文本框显示员工ID,第二个框显示他的工资和文本框显示工资是数字形式而不是文本形式。
我的代码如下:
Hello
I have one combo box with the list of Employee Name from Access DB.And Two Text boxes.I want that when i select a name from combo box ,the first text box show the Employee ID and the the second box show his salary and the text box show salary is in numerical form not in text form.
My code is given below
Imports System.Data.OleDb
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\new.accdb;")
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT EmpName FROM tbl", con)
'ONE COMBO BOX TO SHOW DATA FROM ACCESS LIST
Dim myDA As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "new")
For Each dRow As DataRow In myDataSet.Tables("new").Rows
ComboBox1.Items.Add(dRow.Item("EmpName"))
Next
'TWO TEXT BOXES TO PICK DATA FROM ACCESS LIST ON SELECTION OF COMBO BOX LIST
If ComboBox1.SelectedIndex > -1 Then
Dim dv As DataView = CType(ComboBox1.DataSource, DataView)
For Each drv As DataRowView In dv
If drv.Item("EmpName") = CType(ComboBox1.SelectedItem, DataRowView).Item("EmpName") Then
TextBox1.Text = drv.Item("EmpID").ToString()
TextBox2.Text = drv.Item("Salry").ToString()
Exit For
End If
Next
End If
End Sub
End Class
推荐答案
此代码需要在ComboBox事件中的SelectedIndexChanged。
然后得到index int i = ComboBox1.SelectedIndex;并从DataSet中查找相关行。
您还可以将EmpIds存储在全局列表中,然后使用它来按ID查找Employee。
This code needs to be in the ComboBox event SelectedIndexChanged.
Then get the index int i=ComboBox1.SelectedIndex; and look up the relevant row from the DataSet.
Also you can store the EmpIds in a global list and then use that to look up the Employee by Id.
'TWO TEXT BOXES TO PICK DATA FROM ACCESS LIST ON SELECTION OF COMBO BOX LIST
If ComboBox1.SelectedIndex > -1 Then
Dim dv As DataView = CType(ComboBox1.DataSource, DataView)
For Each drv As DataRowView In dv
If drv.Item("EmpName") = CType(ComboBox1.SelectedItem, DataRowView).Item("EmpName") Then
TextBox1.Text = drv.Item("EmpID").ToString()
TextBox2.Text = drv.Item("Salry").ToString()
Exit For
End If
Next
End If
这篇关于如何从组合框选择填写文本框。请更正我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文