none
how AutoCompleteSource code work with data grid view RRS feed

  • Question

  • hi

     qry1 = kury1
                qry2 = kury2
                Using connection1 As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True")
                    Dim command1 As New SqlCommand(qry1, connection1) '"SELECT * FROM area"
                    Dim table1 As New DataTable
                    Dim adapter1 As New SqlDataAdapter(command1)
                    Dim col As New AutoCompleteStringCollection
                    adapter1.Fill(table1)
                    For i = 0 To table1.Rows.Count - 1
                        col.Add(table1.Rows(i)(qry2).ToString()) '"AreaName"
                    Next
                    Textbox1.AutoCompleteSource = AutoCompleteSource.CustomSource
                    Textbox1.AutoCompleteCustomSource = col
                    Textbox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend

    i want to know,  how this code work with data grid view,  i want to show list of multi column in

    datagrid view. 


    MUHAMMAD ANZAR E-mail : muhammadanzar@hotmail.com Mobile # :0092-3215096959

    Wednesday, October 23, 2019 1:34 PM

All replies

  • Hello,

    A very simple example for auto complete on a specific DataGridViewTextBoxColumn with one field. Not sure what you mean by "list of multi column"

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.Rows.Add("Karen", "Payne")
        End Sub
    
        Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) _
            Handles DataGridView1.EditingControlShowing
    
            If DataGridView1.CurrentCell.ColumnIndex = 1 Then
                Dim tb = CType(e.Control, TextBox)
                tb.AutoCompleteMode = AutoCompleteMode.SuggestAppend
                tb.AutoCompleteSource = AutoCompleteSource.CustomSource
                tb.AutoCompleteCustomSource = New AutoCompleteStringCollection() From {"Payne", "Pie", "Pizza"}
            End If
    
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, October 23, 2019 2:49 PM
    Moderator
  • Hi

    Difficult to understand your question. If Karen answer is not what youwant, here is my understanding. This example needs a DataGridView1 and a TextBox1 on Form1, copy/replace the default code with this code.

    This example fills the DataTable (Table1) with test data, you have your own data source. Once the Table1 is filled, the data is displayed in the DataGridView and is also used to create the AutoCompleteSource for the TextBox1. 

    ' Form1 with DataGridView1
    ' and TextBox1
    Option Strict Off
    Option Explicit On
    Public Class Form1
    	Dim TestPath As String = "C:\Users\lesha\Desktop\TestData..csv"
    	Dim table1 As New DataTable("Freddy")
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		' ============================
    		' set up test data in DataTable
    		' you have your own data but both
    		' end in Table1 filled with data
    		With table1
    			.Columns.Add("AreaName")
    			.Columns.Add("Date")
    			.Columns.Add("Amount")
    		End With
    
    		For Each line As String In IO.File.ReadAllLines(TestPath)
    			Dim a() As String = line.Split(New String() {","}, StringSplitOptions.RemoveEmptyEntries)
    			table1.Rows.Add(a(0), CDate(Trim(a(1))), CDec(a(2)))
    		Next
    		' ============================
    
    
    		' set DGV datasource to Table1
    		DataGridView1.DataSource = table1
    
    		Dim col As New AutoCompleteStringCollection
    		For i As Integer = 0 To table1.Rows.Count - 1
    			col.Add(table1.Rows(i)("AreaName").ToString())
    		Next
    		TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
    		TextBox1.AutoCompleteCustomSource = col
    		TextBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
    
    	End Sub
    End Class


    Regards Les, Livingston, Scotland

    Wednesday, October 23, 2019 3:17 PM
  • Hi,

    Is this effect that you want?

    If so, your can try my code as follows.

    Hope it be helpful.

    Imports System.Data.SqlClient
    
    Public Class Form1
        Private constr As String = "Data Source = (localdb)\MSSQLLocalDB; Integrated Security = True ;AttachDbFileName= C:\Users\juliex\Desktop\Book.mdf"
        Private conn As SqlConnection
        Private sda As SqlDataAdapter
        Private cmd As SqlCommand
        Private dt As New DataTable
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dgv.Location = New Point(50, 50)
            dgv.Size = New Size(Me.Size.Width - 200, Me.Size.Height - 200)
    
            Using conn = New SqlConnection(constr)
                conn.Open()
                sda = New SqlDataAdapter("Select * From student", conn)
                dt.Clear()
                sda.Fill(dt)
                dgv.DataSource = dt
                dgv.EditMode = DataGridViewEditMode.EditOnEnter
                dgv.Columns(0).[ReadOnly] = True
            End Using
        End Sub
    
        Private Sub dgv_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dgv.EditingControlShowing
            Dim sdept As String = dgv.Columns(dgv.CurrentCell.ColumnIndex).HeaderText
            Dim tb As TextBox = TryCast(e.Control, TextBox)
    
            If Not sdept.Equals("sdept") OrElse tb Is Nothing Then
                Return
            End If
    
            tb.AutoCompleteMode = AutoCompleteMode.Suggest
            tb.AutoCompleteSource = AutoCompleteSource.CustomSource
            Dim acsc As AutoCompleteStringCollection = New AutoCompleteStringCollection()
            acsc.Add("asdfklasdjkf")
            acsc.Add("asdfasdf")
            acsc.Add("asdfasdfsad")
            tb.AutoCompleteCustomSource = acsc
        End Sub
    End Class

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Alex Li-MSFT Thursday, October 31, 2019 2:42 AM
    Thursday, October 24, 2019 9:32 AM
    Moderator
  • Hello,

    I'm checking in to see how you are doing with this task.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, October 28, 2019 1:06 PM
    Moderator