Datagridview使用Dropdownmenu(作为Combobox列),其中用户输入要在db中搜索的值

时间:2013-08-29 05:52:18

标签: datagridview

我是vb.net开发问题的新手。我想用dropdownmenu(作为Combobox Column)构建一个datagrid / datagridview,用户在db / dataset / datatable中输入要搜索的值。 用户输入3个字符/数字后,搜索过程开始。 任何帮助? 谢谢

我的基本代码有点:

Imports System.Data.SqlClient
Imports System
Imports System.Collections
Imports System.Windows.Forms
Imports System.Text

Public Class Form2

Dim connetionString As String = "Data Source=sql-server;Initial Catalog=NewTest;Integrated Security=SSPI"
Dim connection As SqlConnection = New SqlConnection(connetionString)


'Create Dataset
Dim ds As New DataSet

'Create Data Adapter
Dim ItemNoAdapter As SqlDataAdapter

'Create Combobox
Dim ComboBox1 As ComboBox
    Dim dt As DataTable
    Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    Me.DataGridView2.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() _
    {Me.ItemNo, Me.Item})


    Me.DataGridView2.AllowUserToOrderColumns = False
    'Using For Not Re-Ordering And Re-Indexing Columns (Data Coming From DB,And Other Columns)
    Me.DataGridView2.AutoGenerateColumns = False
    Me.DataGridView2.BackgroundColor = System.Drawing.SystemColors.Control
    Me.DataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
    Me.DataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
    '
    'ItemNo
    '
    Me.ItemNo.DataPropertyName = "ItemNo"
    Me.ItemNo.HeaderText = "ItemNo"
    Me.ItemNo.Name = "ItemNo"
    Me.ItemNo.MinimumWidth = 150
    Me.ItemNo.Width = 200
    Me.ItemNo.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
    '
    'Item
    '
    Me.Item.DataPropertyName = "Item"
    Me.Item.HeaderText = "Item"
    Me.Item.Name = "Item"
    Me.Item.ReadOnly = True
    '
    'Using For Autosize Column Width Related To Cell Content
    Me.DataGridView2.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.DisplayedCells

    Me.ItemNo.DefaultCellStyle.WrapMode = DataGridViewTriState.True
    'Me.ItemNoDataGridViewComboBoxColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing



    ItemNoAdapter = New SqlDataAdapter("select distinct (ItemNo+'    '+EnglishName) as ItemNo" _
    & " from Products", connection)
    'ItemNoAdapter = New SqlDataAdapter("select ItemNo from Products where ItemNo like '%'" + +"'%'", connection)


    'Create and fill Dataset
    ItemNoAdapter.Fill(ds, "Products")
    'Get the Data Table
    dt = ds.Tables("Products")

    Try
        connection.Open()
        'MessageBox.Show("Results From Products Table")

    Catch ex As Exception
        'MsgBox(ex.ToString)
    Finally
        connection.Close()
        'MessageBox.Show("Connection Closed")
    End Try


    ' Adds the event and the event handler for the method that will 
    ' process the timer event to the timer. 
    'AddHandler DataGridView1.EditingControlShowing, AddressOf DataGridView1_EditingControlShowing

    AddHandler DataGridView2.EditingControlShowing, AddressOf DataGridView2_EditingControlShowing
End Sub

Private Sub DataGridView2_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView2.EditingControlShowing
    If DataGridView2.CurrentCellAddress.X > 0 Then Exit Sub

            'ComboBox1 = CType(e.Control, ComboBox)

    ComboBox1 = TryCast(e.Control, ComboBox)

    If (ComboBox1 IsNot Nothing) Then




                    RemoveHandler ComboBox1.KeyDown, AddressOf ComboBox1_KeyDown
        AddHandler ComboBox1.KeyDown, AddressOf ComboBox1_KeyDown
    End If

End Sub



    Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)


    With ComboBox1
        .DataSource = dt
        .DisplayMember = "ItemNo"
        .ValueMember = "ItemNo"

        'Change Style To DropDown, To Allow For Data Entry
        .DropDownStyle = ComboBoxStyle.DropDown
        '.DropDownStyle = ComboBoxStyle.DropDownList
        .AutoCompleteMode = AutoCompleteMode.SuggestAppend
        .AutoCompleteSource = AutoCompleteSource.ListItems
        '.BackColor = Color.Aqua
    End With




End Sub

End Class

0 个答案:

没有答案