none
SQL + VB windows 窗体 实现三级联动下拉菜单 RRS feed

  • 问题

  • 例如:省,市,区的三级联动
    Microsoft Windows Live Chinese Community - Leo Chen [Stationmaster] Microsoft Windows Live MVP | Windows Live Butterfly
    2010年12月7日 6:23
    版主

答案

  • Hello Leo Chen,

        Welcome to MSDN Forum.

        This is Rocky,  I already know what you mean, the following is a piece of code:


    Imports System.Data

    Imports System.Data.SqlClient

    Public Class frmCom

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      Com1

      Com2

      Com3

    End Sub

     

    Public Sub Com1()

    Dim conn As SqlConnection = New SqlConnection(“Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;” )

    conn.Open

    Dim da As SqlDataAdapter = New SqlDataAdapter("select * from sheng", conn)

    Dim ds As DataSet = New DataSet

    da.Fill(ds)

    ComboBox1.DataSource = ds

    ComboBox1.DisplayMember = "sname"

    ComboBox1.ValueMember = "sID"

    conn.Close

    End Sub

     

    Public Sub Com2()

    Dim conn1 As SqlConnection = New SqlConnection(“Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;”)

    conn1.Open

    Dim da1 As SqlDataAdapter = New SqlDataAdapter(("select * from shi where sID='"  _

    & (ComboBox1.SelectedValue & "'")), conn1)

    Dim ds1 As DataSet = New DataSet

    da1.Fill(ds1)

    ComboBox2.DataSource = ds1

    ComboBox2.DisplayMember = "ssname"

    ComboBox2.ValueMember = "ssID"

    conn1.Close

    End Sub

     

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

     

    Com2

    End Sub

     

    Public Sub Com3()

    Dim conn3 As SqlConnection = New SqlConnection(“Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;”)

    conn3.Open

    Dim da3 As SqlDataAdapter = New SqlDataAdapter(("select * from xian where ssID='"  _

    & (ComboBox2.SelectedValue & "'")), conn3)

    Dim ds3 As DataSet = New DataSet

    da3.Fill(ds3)

    ComboBox3.DataSource = ds3

    ComboBox3.DisplayMember = "sssname"

    ComboBox3.ValueMember = "sssID"

    conn3.Close

    End Sub

     

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

     

    Com3

    End SubEnd Class

     

    I hope this information will be useful

    Best regards

    Rocky

     

    • 已建议为答案 QLoveQ 2010年12月10日 8:26
    • 已标记为答案 Leo ChenModerator 2010年12月10日 14:13
    • 已编辑 QLoveQ 2010年12月23日 2:58
    2010年12月10日 7:05

全部回复

  • 你好

    你的意思是用VB + SQL 寫一 個3個 RELATED combobox 嗎?

    或者你可以考慮以下方法

    相信在SQL 上 會有適當的 ID FIELD 的 E.G. CITYID, COUNTYID...ETC
    當:省  的combobox 被選是.. 在他的 ONSELECTCHANGE EVENT 上加入 程式碼去 取 ,市 的 資料 和 ENABLE 市這個 combobox ... 當選了市後 ,区

    在他的 ONSELECTCHANGE EVENT 上加入 程式碼去 取 ,区的 資料 和 ENABLE 区這個 combobox 

    應該可以解決這個問題的

    Please correct me if my concept is wrong


    Chi
    2010年12月7日 6:50
    版主
  • 是的,是这个意思。

    因为是初步入门所以有点晕啊!

    在SQL里是不是有一个树型结构,以及在VB2010里,是不是也有了三级联动的开发功能了呢?

    印象中在哪一个PDA里有见过,你有印象吗?


    Microsoft Windows Live Chinese Community - Leo Chen [Stationmaster] Microsoft Windows Live MVP | Windows Live Butterfly
    2010年12月7日 7:16
    版主
  • 你好

    你的意思是不是 sql 2008 hierarchyid data type 呢? 沒有真正用過 :(

     也不知清楚 以及在VB2010里,是不是也有了三级联动的开发功能...

    希望其他高人可以幫忙解決這個問題

     


    Chi
    2010年12月7日 8:34
    版主
  • Hello Leo Chen,

        Welcome to MSDN Forum.

        This is Rocky,  I already know what you mean, the following is a piece of code:


    Imports System.Data

    Imports System.Data.SqlClient

    Public Class frmCom

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      Com1

      Com2

      Com3

    End Sub

     

    Public Sub Com1()

    Dim conn As SqlConnection = New SqlConnection(“Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;” )

    conn.Open

    Dim da As SqlDataAdapter = New SqlDataAdapter("select * from sheng", conn)

    Dim ds As DataSet = New DataSet

    da.Fill(ds)

    ComboBox1.DataSource = ds

    ComboBox1.DisplayMember = "sname"

    ComboBox1.ValueMember = "sID"

    conn.Close

    End Sub

     

    Public Sub Com2()

    Dim conn1 As SqlConnection = New SqlConnection(“Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;”)

    conn1.Open

    Dim da1 As SqlDataAdapter = New SqlDataAdapter(("select * from shi where sID='"  _

    & (ComboBox1.SelectedValue & "'")), conn1)

    Dim ds1 As DataSet = New DataSet

    da1.Fill(ds1)

    ComboBox2.DataSource = ds1

    ComboBox2.DisplayMember = "ssname"

    ComboBox2.ValueMember = "ssID"

    conn1.Close

    End Sub

     

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

     

    Com2

    End Sub

     

    Public Sub Com3()

    Dim conn3 As SqlConnection = New SqlConnection(“Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;”)

    conn3.Open

    Dim da3 As SqlDataAdapter = New SqlDataAdapter(("select * from xian where ssID='"  _

    & (ComboBox2.SelectedValue & "'")), conn3)

    Dim ds3 As DataSet = New DataSet

    da3.Fill(ds3)

    ComboBox3.DataSource = ds3

    ComboBox3.DisplayMember = "sssname"

    ComboBox3.ValueMember = "sssID"

    conn3.Close

    End Sub

     

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

     

    Com3

    End SubEnd Class

     

    I hope this information will be useful

    Best regards

    Rocky

     

    • 已建议为答案 QLoveQ 2010年12月10日 8:26
    • 已标记为答案 Leo ChenModerator 2010年12月10日 14:13
    • 已编辑 QLoveQ 2010年12月23日 2:58
    2010年12月10日 7:05
  • Hello , Leo Chen

    Welcome to MSDN Forum.

    This is  rocky ,  I already know what you mean , the following  is a piece of code :


    Imports System.Data

    Imports System.Data.SqlClient

    Public Class frmCom

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      Com1

      Com2

      Com3

    End Sub

     

    Public Sub Com1()

    Dim conn As SqlConnection = New SqlConnection(“Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;” )

    conn.Open

    Dim da As SqlDataAdapter = New SqlDataAdapter("select * from sheng", conn)

    Dim ds As DataSet = New DataSet

    da.Fill(ds)

    ComboBox1.DataSource = ds

    ComboBox1.DisplayMember = "sname"

    ComboBox1.ValueMember = "sID"

    conn.Close

    End Sub

     

    Public Sub Com2()

    Dim conn1 As SqlConnection = New SqlConnection(“Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;”)

    conn1.Open

    Dim da1 As SqlDataAdapter = New SqlDataAdapter(("select * from shi where sID='"  _

    & (ComboBox1.SelectedValue & "'")), conn1)

    Dim ds1 As DataSet = New DataSet

    da1.Fill(ds1)

    ComboBox2.DataSource = ds1

    ComboBox2.DisplayMember = "ssname"

    ComboBox2.ValueMember = "ssID"

    conn1.Close

    End Sub

     

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

     

    Com2

    End Sub

     

    Public Sub Com3()

    Dim conn3 As SqlConnection = New SqlConnection(“Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;”)

    conn3.Open

    Dim da3 As SqlDataAdapter = New SqlDataAdapter(("select * from xian where ssID='"  _

    & (ComboBox2.SelectedValue & "'")), conn3)

    Dim ds3 As DataSet = New DataSet

    da3.Fill(ds3)

    ComboBox3.DataSource = ds3

    ComboBox3.DisplayMember = "sssname"

    ComboBox3.ValueMember = "sssID"

    conn3.Close

    End Sub

     

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

     

    Com3

    End SubEnd Class

     

    I hope this information will be useful

    Thank you .

    Best regards

    Rocky

     

    Hello, Rocky, thanks for your help and information, and I will to try and feedback to here. Thank you again~


    Microsoft Windows Live Chinese Community - Leo Chen [Stationmaster] Microsoft Windows Live MVP | Windows Live Butterfly
    2010年12月10日 16:24
    版主
  • Hello , Leo Chen

    Welcome to MSDN Forum.

    This is  rocky ,  I already know what you mean , the following  is a piece of code :


    Imports System.Data

    Imports System.Data.SqlClient

    Public Class frmCom

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      Com1

      Com2

      Com3

    End Sub

     

    Public Sub Com1()

    Dim conn As SqlConnection = New SqlConnection(“Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;” )

    conn.Open

    Dim da As SqlDataAdapter = New SqlDataAdapter("select * from sheng", conn)

    Dim ds As DataSet = New DataSet

    da.Fill(ds)

    ComboBox1.DataSource = ds

    ComboBox1.DisplayMember = "sname"

    ComboBox1.ValueMember = "sID"

    conn.Close

    End Sub

     

    Public Sub Com2()

    Dim conn1 As SqlConnection = New SqlConnection(“Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;”)

    conn1.Open

    Dim da1 As SqlDataAdapter = New SqlDataAdapter(("select * from shi where sID='"  _

    & (ComboBox1.SelectedValue & "'")), conn1)

    Dim ds1 As DataSet = New DataSet

    da1.Fill(ds1)

    ComboBox2.DataSource = ds1

    ComboBox2.DisplayMember = "ssname"

    ComboBox2.ValueMember = "ssID"

    conn1.Close

    End Sub

     

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

     

    Com2

    End Sub

     

    Public Sub Com3()

    Dim conn3 As SqlConnection = New SqlConnection(“Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;”)

    conn3.Open

    Dim da3 As SqlDataAdapter = New SqlDataAdapter(("select * from xian where ssID='"  _

    & (ComboBox2.SelectedValue & "'")), conn3)

    Dim ds3 As DataSet = New DataSet

    da3.Fill(ds3)

    ComboBox3.DataSource = ds3

    ComboBox3.DisplayMember = "sssname"

    ComboBox3.ValueMember = "sssID"

    conn3.Close

    End Sub

     

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

     

    Com3

    End SubEnd Class

     

    I hope this information will be useful

    Thank you .

    Best regards

    Rocky

     


    是添加一个CLASS1.VB的类是吗?并COPY这些代码?

    不好意思,是入门者,所以如果可以的话,请给一个详细的说明,谢谢!


    Microsoft Windows Live Chinese Community - Leo Chen [Stationmaster] Microsoft Windows Live MVP | Windows Live Butterfly
    2010年12月11日 8:09
    版主
  •  Hello Leo Chen,

    没有必要重新添加一个类class1.vb,因为那样你在调用的时候还得 new 一个对象。在使用上面的代码的时候, 在 你的窗体比如说form1 ,右键或者 双击form1, 就会进入到代码编辑窗口,然后 你可以 使用 ctrl + c ctrl +v 了。

    祝你成功! 

    2010年12月13日 1:52
    版主
  •  Hello Leo Chen,

    没有必要重新添加一个类class1.vb,因为那样你在调用的时候还得 new 一个对象。在使用上面的代码的时候, 在 你的窗体比如说form1 ,右键或者 双击form1, 就会进入到代码编辑窗口,然后 你可以 使用 ctrl + c ctrl +v 了。

    祝你成功! 


    谢谢你的答复,我后来也研究明白了。

    不过,上面的代码好像有一些问题。我已经另开了一个贴子讨论,谢谢!


    Microsoft Windows Live Chinese Community - Leo Chen [Stationmaster] Microsoft Windows Live MVP | Windows Live Butterfly
    2010年12月13日 2:38
    版主