如何使用组合框执行此操作 [英] how to do this using combo box
本文介绍了如何使用组合框执行此操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要这个答案
我已经将所有数据存储在sql server中。当我在组合框中选择数据时,数据库中其他值如文本框值将显示在相应的文本框上。
解决方案
使用Combobox OnselectedIndexChanged事件编写程序,无论您选择哪个值设置那些适当的值在文本框中。
您可以在ComboBox上编写代码OnSelectedIndexChanged
事件以使用适当的值填充TextBox。您可以为此编写自己的可重用功能。请参阅以下代码:
''' < 摘要 >
''' 基于ComboBox中选择的值的Fille TextBox。
''' < / summary >
''' < param < span class =code-summarycomment> name =SelectField > 表列要在TextBox中显示的名称字符串< / param > ;
''' < param name =TableName > 表名为字符串< / param >
''' < param name =ColumnName > 表格列名为Where子句< / param >
''' < param name =ComboBoxName > 用于Where子句条件的组合框名称< / param >
''' < param name =SetTextBoxName > TextBox名称填充值< < span class =code-summarycomment> / param >
' '' < 备注 > < / remarks >
公开 Sub FillTextBox( ByVal SelectField As String , ByVal TableName 作为 字符串, ByVal ColumnName 作为 字符串,< span class =code-keyword> ByVal ComboBoxName As ComboBox, ByVal SetTextBoxName As TextBox)
Dim _ObjDataAdapter As SqlDataAdapter = 新 SqlDataAdapter
_ObjDataAdapter.SelectCommand = 新 SqlCo mmand
_ObjDataAdapter.SelectCommand.Connection = _Connection ' 您的SqlConnection对象
_ObjDataAdapter。 SelectCommand.CommandText = 选择& SelectField& FROM& TableName& WHERE& ColumnName& ='& ComboBoxName.Text& '
尝试
_ObjDataReader = _ObjDataAdapter.SelectCommand.ExecuteReader()' Exectuing query
Catch ex As System.InvalidOperationException
MessageBox.Show(ex.ToString)' 提供错误消息
结束 尝试
_ObjDataReader.Read ' 读取DataReader项
SetTextBoxName.Text = _ObjDataReader.Item( 0 )' 添加将这些项目放入ComboBox
结束
_ObjDataReader.Close ()' 关闭DataReader对象
_ObjDataAdapter.SelectCommand = 没有
结束 Sub
之后你可以在ComboBox上调用这个函数OnSelectedIndexChanged
事件
FillTextBox( DisplayColumn, TableName, WhereClauseColumn,ComboBox1,TextBox1)
我希望这会对你有所帮助。 :)
hi,
i want the answer for this
i already store all data's in sql server. when i select data in combobox the database other values like textbox values will show on appropriate textbox.
解决方案
Use Combobox OnselectedIndexChanged event write the program whatever value you have selected set those value in appropriate in text box.
You can write your code on ComboBoxOnSelectedIndexChanged
Event to fill your TextBox with appropriate value. You can write your own reusable function for this. See the below code :
''' <summary> ''' Fille TextBox Based On Value Selected in ComboBox. ''' </summary> ''' <param name="SelectField">Table Column Name To Display In TextBox As String</param> ''' <param name="TableName">Table Name As String</param> ''' <param name="ColumnName">Table Column Name For Where Clause</param> ''' <param name="ComboBoxName">ComboBox Name For Where Clause Condition</param> ''' <param name="SetTextBoxName">TextBox Name To Fill Value</param> ''' <remarks></remarks> Public Sub FillTextBox(ByVal SelectField As String, ByVal TableName As String, ByVal ColumnName As String, ByVal ComboBoxName As ComboBox, ByVal SetTextBoxName As TextBox) Dim _ObjDataAdapter As SqlDataAdapter = New SqlDataAdapter _ObjDataAdapter.SelectCommand = New SqlCommand _ObjDataAdapter.SelectCommand.Connection = _Connection 'Your SqlConnection Object _ObjDataAdapter.SelectCommand.CommandText = "Select " & SelectField & " FROM " & TableName & " WHERE " & ColumnName & "='" & ComboBoxName.Text & "'" Try _ObjDataReader = _ObjDataAdapter.SelectCommand.ExecuteReader() 'Exectuing query Catch ex As System.InvalidOperationException MessageBox.Show(ex.ToString) 'Providing Error Message End Try While _ObjDataReader.Read 'Reading DataReader Items SetTextBoxName.Text = _ObjDataReader.Item(0)'Adding these Items Into ComboBox End While _ObjDataReader.Close() 'Closing DataReader Object _ObjDataAdapter.SelectCommand = Nothing End Sub
After that you can call this Function on ComboBoxOnSelectedIndexChanged
Event
FillTextBox("DisplayColumn", "TableName", "WhereClauseColumn", ComboBox1, TextBox1)
I hope this will help you. :)
这篇关于如何使用组合框执行此操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文