vb.net组合框手动和从db加载数据

时间:2018-02-10 08:48:46

标签: sql-server-2008 combobox vb.net-2010 loaddata

Private Sub delivery_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)处理MyBase.Load

    sel.Items.Add("allitems")

    MsgBox("alert")
    Me.EntryTableAdapter.Fill(Me.TailerDataSet.entry)
    viewdata.Visible = False
    ConnectionState = "Data Source=SWTEAM-II-5\SQLEXPRESS;Initial Catalog=tailer;User ID=sa;Password=123"
    con = New SqlConnection(ConnectionState)
    con.Open()

    Dim sqlquery As String
    sqlquery = "Select p_name from entry"
    cmd = New SqlCommand(sqlquery, con)
    Dim rd As SqlDataReader = cmd.ExecuteReader
    Dim dt As DataTable = New DataTable


    dt.Load(rd)
    dt.Rows.Add("allitems")
    sel.ValueMember = "p_name"
    sel.DisplayMember = "p_name"
    sel.DataSource = dt

只有组合框显示db值而非手动值

1 个答案:

答案 0 :(得分:0)

 Dim sqlquery As String

 sqlquery = "Select p_name from entry"

 cmd = New SqlCommand(sqlquery, con)

 Dim adapter As New SqlDataAdapter(cmd)

sel.Items.Add("allitems")

Dim dap As New SqlDataAdapter("SELECT * FROM entry", con)

  Dim ds As New DataSet

  dap.Fill(ds)

 For i As Integer = 0 To ds.Tables(0).Rows.Count - 1

  sel.Items.Add(ds.Tables(0).Rows(i).Item("p_name"))

Next