vb.net combobox人口获得第一个组合框的索引 [英] vb.net combobox population getting index of the 1st combobox

查看:481
本文介绍了vb.net combobox人口获得第一个组合框的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在载入时用这个代码填充我的combobox1

  sql =select name1,id1 from table1
da = New Odbc.OdbcDataAdapter(sql,con)
da.Fill(ds,cbtbl1)
ComboBox1.DataSource = ds.Tables(cbtbl1)
ComboBox1.DisplayMember = ds .Tables(cbtbl1)。Columns(name1)。Caption

我有我的第二个combobox2相关combobox1。我在combobox1 selectedvaluechanged 中插入了此代码。这将根据它们的相关id更改combobox2的值

  sql =select name2,id2 from table2 where id1 =& ; ?? 
da = New Odbc.OdbcDataAdapter(sql,con)
da.Fill(ds,cbtbl2)
ComboBox2.DataSource = ds.Tables(cbtbl2)
ComboBox2 .DisplayMember = ds.Tables(cbtbl2)。Columns(name2)。Caption

我的代码我有问号。它应该是table1的id,我不知道如何得到(或什么放

解决方案

将Combobox1的 ValueMember 设置为从数据库中检索的ID,并使用 SelectedValue 属性检索ID



我不认为它会工作,除非你指定 ValueMember



好的,我使用我目前正在处理的数据库快速敲敲一些东西(它是OLEDB,但应该't matter for this)

  Public Sub New()

'设计器
InitializeComponent()

在InitializeComponent()调用之后添加任何初始化

Dim ds As New DataSet()
Dim test As新的OleDbDataAdapter(SELECT [ID],[名前] FROM [Tレイヤ管理],DBConnections.PrimaryAccessDBConnection)
调用test.Fill(ds,testTable)

Me.ComboBox1 .DataSource = ds.Tables(testTable)
Me.ComboBox1.ValueMember =ID
Me.ComboBox1.DisplayMember =名前

AddHandler Me.ComboBox1。 SelectedValueChanged,AddressOf某事

结束子

私有子事物(作为对象的发送者,作为EventArgs)

调用MessageBox.Show(String.Format (ID {0},Me.ComboBox1.SelectedValue))

结束子


$ b b

我得到的ID显示正好与此。



更新:



那么你可以通过这种方式获得所选择的项:



私有子对象(作为对象的发送者,作为EventArgs)

  Dim selectedItem As DataRowView = CType(Me.ComboBox1.SelectedItem,DataRowView)

调用MessageBox.Show(String.Format }

End Sub
/ pre>

I populated my combobox1 with this code in load

sql = "select name1,id1 from table1"
da = New Odbc.OdbcDataAdapter(sql, con)
da.Fill(ds, "cbtbl1")
ComboBox1.DataSource = ds.Tables("cbtbl1")
ComboBox1.DisplayMember = ds.Tables("cbtbl1").Columns("name1").Caption

I have my 2nd combobox2 related to combobox1. I inserted this code in combobox1 selectedvaluechanged. This to change to value of combobox2 based on their related ids

sql = "select name2,id2 from table2 where id1=" & ???????
da = New Odbc.OdbcDataAdapter(sql, con)
da.Fill(ds, "cbtbl2")
ComboBox2.DataSource = ds.Tables("cbtbl2")
ComboBox2.DisplayMember = ds.Tables("cbtbl2").Columns("name2").Caption

In my code i have question marks. It supposed to be the id of table1 which i don't know how to get :( or what to put

解决方案

You should set the ValueMember of Combobox1 to be the ID you retrieved from the database and the use the SelectedValue property to retrieve the ID of the selected item.

I don't think it's gonna work unless you specify the ValueMember property when you databind Combobox1, so don't forget to do that first.

OK, I knocked something together quickly with a database I am working on at the moment (it's OLEDB, but shouldn't matter for this)

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    Dim ds As New DataSet()
    Dim test As New OleDbDataAdapter("SELECT [ID], [名前] FROM [Tレイヤ管理]", DBConnections.PrimaryAccessDBConnection)
    Call test.Fill(ds, "testTable")

    Me.ComboBox1.DataSource = ds.Tables("testTable")
    Me.ComboBox1.ValueMember = "ID"
    Me.ComboBox1.DisplayMember = "名前"

    AddHandler Me.ComboBox1.SelectedValueChanged, AddressOf Something

End Sub

Private Sub Something(sender As Object, e As EventArgs)

    Call MessageBox.Show(String.Format("ID {0}", Me.ComboBox1.SelectedValue))

End Sub

I get the ID showing just fine with this.

UPDATE:

If this still doesn't work then you can get the selected item this way:

Private Sub Something(sender As Object, e As EventArgs)

    Dim selectedItem As DataRowView = CType(Me.ComboBox1.SelectedItem, DataRowView)

    Call MessageBox.Show(String.Format("ID {0} Name {1}", New Object() {selectedItem("ID"), selectedItem("名前")}))

End Sub

这篇关于vb.net combobox人口获得第一个组合框的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆