• We just launched and are currently in beta. Join us as we build and grow the community.

AutoComplete TextBox in a DataGridView

dragorixvlad

Story Format Specialist
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
37
Likes
57
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial I will teach you how to Auto Complete a TextBox inside the DataGridView in the Visual Basic 2008 and MySQL Database. Others are used to store the list of items in the ComboBox. So, for a change, I used a TextBox because it’s easy to use and you don’t have to drop down in searching for the items that you needed. Just simply type the initial letter of the items that you’re going to search. I based this on my tutorials which are the “Simple AutoComplete ComboBox in a DataGridView” and “Advance Autocomplete/Autosuggest in a Textbox Using Visual Basic 2008 and MySQL database”.

To start with:

1. Create a database.

  1. CREATE

    DATABASE

    `test`

2. Create a Table.

  1. CREATE

    TABLE

    [url=http://dev.mysql.com/doc/refman/%35%2E%31/en/control-flow-functions.html]IF

    NOT

    EXISTS

    [/url] `item`

    (
  2. `id`

    int

    (

    11

    )

    NOT

    NULL

    ,
  3. PRIMARY KEY

    (

    `id`

    )
  4. )

    ENGINE

    =

    MyISAM DEFAULT

    CHARSET

    =

    utf8;

3. Insert this records in the table that you have created.

  1. INSERT

    INTO

    `item`

    (

    `id`

    )

    VALUES


  2. (

    213

    )

    ,

    (

    190

    )

    ,
  3. (

    102

    )

    ,
  4. (

    103

    )

    ,
  5. (

    104

    )

    ,
  6. (

    107

    )

    ,
  7. (

    124

    )

    ,
  8. (

    141

    )

    ,
  9. (

    142

    )

    ,
  10. (

    143

    )

    ,
  11. (

    144

    )

    ,
  12. (

    149

    )

    ,
  13. (

    174

    )

    ,
  14. (

    176

    )

    ,
  15. (

    178

    )

    ,
  16. (

    200

    )

    ,
  17. (

    201

    )

    ,
  18. (

    205

    )

    ,
  19. (

    206

    )

    ,
  20. (

    207

    )

    ,
  21. (

    100

    )

4. Open the Visual Basic 2008, Create a project and create a Form. In the Form, add the DataGridView.

fform_2.jpg


5. Double click the Form and above the Form1_Load

, set a connection of MySQL Database to Visual Basic 2008. After that, declare all the classes and variables that you needed.

  1. 'add reference
  2. Imports

    MySql.

    Data

    .

    MySqlClient
  3. Public

    Class

    Form1

  4. 'set up the string connection of MySQL Database
  5. Dim

    con As

    MySqlConnection =

    New

    MySqlConnection(

    "server=localhost;user id=root;database=employeesdb"

    )
  6. 'a set of commands in MySQL
  7. Dim

    cmd As

    New

    MySqlCommand
  8. 'a Bridge between a database and datatable for retrieving and saving data.
  9. Dim

    da As

    New

    MySqlDataAdapter
  10. 'a specific table in the database
  11. Dim

    dt As

    New

    DataTable


  12. Private

    Sub

    Form1_Load(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    MyBase

    .

    Load

  13. End

    Sub
  14. End

    Class

6. In the Form1_Load

, declare the variable txt as a TextBox Column and add it in the DataGridView.

  1. Private

    Sub

    Form1_Load(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    MyBase

    .

    Load
  2. 'declaring a new textbox column in the datagridview
  3. Dim

    txt As

    New

    DataGridViewTextBoxColumn
  4. 'adding a textbox column in the datagridview
  5. DataGridView1.

    Columns

    .

    Add

    (

    txt)
  6. End

    Sub

7. Go back to the Design Views, double click the DataGridview and change the method name in EditingControlShowing.

method_name_0.jpg


8. In the DataGridView1_ EditingControlShowing

, do the following code for storing data in the TextBox and the autocomplete properties of it.

  1. Private

    Sub

    DataGridView1_EditingControlShowing(

    ByVal

    sender As

    Object

    , ByVal

    e As

    System.

    Windows

    .

    Forms

    .

    DataGridViewEditingControlShowingEventArgs

    )

    Handles

    DataGridView1.

    EditingControlShowing
  2. 'declare for a new textbox
  3. Dim

    txt As

    New

    TextBox
  4. con.

    Open

    (

    )
  5. 'set a new spicific table in the database
  6. dt =

    New

    DataTable
  7. 'set your commands for holding the data.
  8. With

    cmd
  9. .

    Connection

    =

    con
  10. .

    CommandText

    =

    "Select * from employees"
  11. End

    With
  12. 'filling the table in the database
  13. da.

    SelectCommand

    =

    cmd
  14. da.

    Fill

    (

    dt)

  15. Dim

    r As

    DataRow 'represents a row of data in the datatable
  16. For

    Each

    r In

    dt.

    Rows

    'get a collection of rows that belongs to this table

  17. 'the control shown to the user for editing the selected cell value
  18. If

    TypeOf

    e.

    Control

    Is

    TextBox Then
  19. txt =

    e.

    Control
  20. 'adding the specific row of the table in the AutoCompleteCustomSource of the textbox
  21. txt.

    AutoCompleteCustomSource

    .

    Add

    (

    r.

    Item

    (

    "EMPLOYEE_ID"

    )

    .

    ToString

    )
  22. txt.

    AutoCompleteMode

    =

    AutoCompleteMode.

    Suggest
  23. txt.

    AutoCompleteSource

    =

    AutoCompleteSource.

    CustomSource
  24. End

    If
  25. Next
  26. 'closing the connection
  27. con.

    Close

    (

    )
  28. End

    Sub


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

326,766

326,774

Top