DataGridView AutoComplete TextBox

In this article we will discuss how to utilize the AutoComplete Feature in a DataGridView Column that acts as a TextBox.

DataGridView-AutoComplete.jpg

Introduction

There tends to exist cases where you need to use the AutoComplete Feature in a DataGridView column. So we will try to learn how to do this.

Here is the procedure to be followed.

1. Create a Windows Application and add a DataGridView control to the Form.

2. In the Form Load Event paste this piece of code.

    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.ColumnCount = 2;
        dataGridView1.Columns[0].Name = "No.";
        dataGridView1.Columns[1].Name = "Name";
    }


Explanation

dataGridView1.ColumnCount = 2;

Here we set the number of columns to be displayed in the DataGridView Control.

dataGridView1.Columns[0].Name = "No.";
dataGridView1.Columns[1].Name = "Name";

Here we set the Name of the Columns. (This is the name that will be displayed in the header of the DataGridView control)

3. Create a method that contains a list of strings that we use for the auto-complete feature.

public AutoCompleteStringCollection AutoCompleteLoad()
{
    AutoCompleteStringCollection str = new AutoCompleteStringCollection();
    for (int i = 0; i <=5; i++)
        str.Add("Name " + i.ToString());
     return str;
}

Explanation

AutoCompleteStringCollection str = new AutoCompleteStringCollection();

Creating an instance of the AutoCompleteStringCollection class that will hold the collection of strings for the auto-complete feature.

for (int i = 0; i <=5; i++)
str.Add("Name " + i.ToString());


Here we create a self generated loop to add items to the AutoCompleteStringCollection object using the Add() method.

public AutoCompleteStringCollection AutoCompleteLoad()

Here we declare the method with return type AutoCompleteStringCollection since it returns a collection.

4. Now, we need the code for the Event EditingControlShowing of the DataGridView control:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

{

    int column = dataGridView1.CurrentCell.ColumnIndex;

    string headerText = dataGridView1.Columns[column].HeaderText;

 

    if (headerText.Equals("Name"))

    {

        TextBox tb = e.Control as TextBox;

 

        if (tb != null)

        {

            tb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

            tb.AutoCompleteCustomSource = AutoCompleteLoad();

            tb.AutoCompleteSource = AutoCompleteSource.CustomSource;                   

        }

    }

}

Explanation

EditingControlShowing Event – is trigged/occurs when the editing for the cell is shown.

string headerText = dataGridView1.Columns[column].HeaderText;

Here, we get the Header Text for the column being edited.

Since we need to provide the auto-complete feature for only the DataGridView column Name we test it.

tb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
tb.AutoCompleteCustomSource = AutoCompleteLoad();
tb.AutoCompleteSource = AutoCompleteSource.CustomSource;

The AutoCompleteMode specifies how the text is proposed in a TextBox. The possible Enumerations for AutoCompleteMode are Append, Suggest, SuggestAppend and None.

The AutoCompleteMode Enumeration SuggestAppend displays a list of values with the first value appended in the TextBox.

The AutoCompleteCustomSource defines the Stringcollection to use when the AutoCompleteSource is set to AutoCompleteSource.CustomSource.

The AutoCompleteSource has enumerations that can be set to an AutoCompleteSource enumeration, FileSystem, HistoryList, RecentlyUsedList, AllUrl, AlSystemSources, FileSystemDirectories, CustomSource or None. Since we are dealing with our own collection we will set it to CustomSource.

5. Run the project and try adding new rows to the DataGridView, when entering data into the column Name you will get the AutoComplete Feature "ON" for the TextBox.

Code Snippet

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace AutoCompleteTextBoxinDG

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            dataGridView1.ColumnCount = 2;

            dataGridView1.Columns[0].Name = "No.";

            dataGridView1.Columns[1].Name = "Name";

        }

 

        private void bt_close_Click(object sender, EventArgs e)

        {

            this.Close();

        }

 

        public AutoCompleteStringCollection AutoCompleteLoad()

        {

            AutoCompleteStringCollection str = new AutoCompleteStringCollection();

 

            for (int i = 0; i <=5; i++)

                str.Add("Name " + i.ToString());

 

            return str;

        }

 

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

        {

            int column = dataGridView1.CurrentCell.ColumnIndex;

            string headerText = dataGridView1.Columns[column].HeaderText;

 

            if (headerText.Equals("Name"))

            {

                TextBox tb = e.Control as TextBox;

 

                if (tb != null)

                {

                    tb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

                    tb.AutoCompleteCustomSource = AutoCompleteLoad();

                    tb.AutoCompleteSource = AutoCompleteSource.CustomSource;                    

                }

 

            }

        }

    }

}


Article Roundup

In this article we discussed how to utilize the auto-complete feature in a DataGridView control in Windows Forms when editing the column.

Up Next
    Ebook Download
    View all
    Learn
    View all