SiteMap  | About    

DataGridView Autocomplete TextBox

Dispalying data in a tabular format is a task you are likely to perform regularly. The VB.Net DataGridView control is highly configurable and extensible, and it allows you to customize its display and behavior using properties, methods, and events.

DataGridView Autocomplete TextBox in C#

VB.Net DataGridView

C# AutoComplete in DataGridView

In VB.Net you can display DataGridView in Bound mode, unbound mode and Virtual mode . Bound mode is suitable for managing data using automatic interaction with the data source. The data can be displayed in the DatagridView using TextBox, ComboBox, ListBox etc. While you display the data on TextBox, you can make your TextBoxes as Autocomplete TextBox in VB.Net.

AutoComplete in DataGridView

C# Autocomplete TextBox

The VB.Net TextBox properties like AutoCompleteCustomSource, AutoCompleteMode and AutoCompleteSource to perform automatically completes user input strings by comparing the prefix letters being entered to the prefixes of all strings in a data store.

The following VB.Net program shows how to create an AutoComplete Textbox inside a DataGridView.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridView1.ColumnCount = 3
        DataGridView1.Columns(0).Name = "Product ID"
        DataGridView1.Columns(1).Name = "Product Name"
        DataGridView1.Columns(2).Name = "Product Price"

        Dim row As String() = New String() {"1", "Product 1", "1000"}
        row = New String() {"2", "Product 2", "2000"}
        row = New String() {"3", "Product 3", "3000"}
        row = New String() {"4", "Product 4", "4000"}
    End Sub

    Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        Dim titleText As String = DataGridView1.Columns(1).HeaderText
        If titleText.Equals("Product Name") Then
            Dim autoText As TextBox = TryCast(e.Control, TextBox)
            If autoText IsNot Nothing Then
                autoText.AutoCompleteMode = AutoCompleteMode.Suggest
                autoText.AutoCompleteSource = AutoCompleteSource.CustomSource
                Dim DataCollection As New AutoCompleteStringCollection()
                autoText.AutoCompleteCustomSource = DataCollection
            End If
        End If
    End Sub

    Public Sub addItems(ByVal col As AutoCompleteStringCollection)
        col.Add("Product 1")
        col.Add("Product 2")
        col.Add("Product 3")
        col.Add("Product 4")
        col.Add("Product 5")
        col.Add("Product 6")
    End Sub

End Class (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.