从所选项目组合框中取回值
我有这个子程序来填充一些组合框
I have this subroutine to fill some comboboxes
Private Sub Fill_Combo_Box(ByVal ParmSQL As String, ByVal ComboName As ComboBox)
Dim Con As OleDbConnection
Dim sqlCmd As OleDbCommand = New OleDbCommand(ParmSQL)
Con = New OleDbConnection(Main_Form.ConString)
Try
Con.Open()
sqlCmd.Connection = Con
myData = sqlCmd.ExecuteReader
Do While myData.Read
ComboName.ValueMember = myData.Item(0)
ComboName.Items.Add(myData.Item(1).ToString.PadRight(10, " ") & " " & myData.Item(2))
Loop
Catch ''ex As OledbException
End Try
Con.Close()
End Sub
子例程工作很好,但是请随时帮助我如何处理
来自selecteditem的值?
谢谢,问候...
The subroutine work great but please help me how i can take at any time the
value from the selecteditem??
Thanks and regards...
如果您只想选择所选项目的文本,则可以使用
If you just want the text of the Item that was selected, you can use
<br />
ComboName.SelectedItem.Text;<br />
如果您需要实际值而不是文本:
if you want the actual value and not the text:
<br />
ComboName.SelectedValue;<br />
您应该在comboBox的SelectedIndexChanged事件上获取值.
我希望这是您想知道的.祝你好运,我的朋友
You should get the values on the SelectedIndexChanged Event of your comboBox.
I hope this is what you wanted to know. Good Luck my friend
好,首先,您应该使用OleDbAdapter和数据集而不是阅读器.您的代码如下所示:
Ok, first you should use OleDbAdapter and a dataset instead of the reader. Your code would look like this:
Private Sub Fill_Combo_Box(ByVal ParmSQL As String, ByVal ComboName As ComboBox) Dim Con As OleDbConnection
Dim adapter as OleDbAdapter
Dim dataSet as DataSet
Dim sqlCmd As OleDbCommand = New OleDbCommand(ParmSQL)
Con = New OleDbConnection(Main_Form.ConString)
Try
Con.Open()
sqlCmd.Connection = Con
adapter.SelectCommand = sqlCmd
adapter.Fill(dataSet)
'Then we set the dataSource to the dataset's table
ComboName.DataSource = dataSet.Tables(0)
ComboName.ValueMember = "NameOfFirstColumn"
ComboName.DisplayMember = ("NameOfSecondColumn").PadRight(10, " ") & " " & "NameOfThirdColumn"
Catch 'ex As OledbException End Try Con.Close()End Sub
我不太确定displayMember是一个有效的表达式,我猜不是,但是也许您可以在sql表中添加一个具有所需格式的文本的新列,然后您只需说:
ComboName.DisplayMember =(" NameOfNewColumnWithTheFormat)
同时,通过使用SelectedItem.Value,您可以仅使用valuemember进行测试,而不会与displayMember混淆.祝你好运
I''m not too sure about the displayMember being a valid expression, I''m guessing not but maybe you could add a new column to your sql table that has the text in the format you want and then you''d just say:
ComboName.DisplayMember = ("NameOfNewColumnWithTheFormat")
Meanwhile you can test just with the valuemember and not mess with the displayMember by using SelectedItem.Value. Good Luck