articles

Home / DeveloperSection / Articles / DataGridView AutoComplete TextBox

DataGridView AutoComplete TextBox

Shankar M16418 12-Apr-2013

In this article we will discuss how to achieve AutoComplete Feature in DataGridView Column which acts as a TextBox.

DataGridView AutoComplete TextBox

Introduction

There tends to be possible cases where you need to use AutoComplete Feature in DataGridView column. So we will try to learn how to achieve this.

Here are the Steps 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 which will be displayed in the header of the DataGridView control)

 

       3.     Creating a method that contains a list of Strings that we use for 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 AutoCompleteStringCollection class that will hold the collection of strings for 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 Add() method.

public AutoCompleteStringCollection AutoCompleteLoad()

Here we declared the method with return type AutoCompleteStringCollection which mean it returns a collection.

       4.     Now, we need code the Event EditingControlShowing of 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/occured 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 auto-complete Feature to only 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, None.

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 it 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 in 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 have discussed how to achieve auto-complete Feature in the DataGridView control in Windows Forms when editing the column.


Updated 09-Jul-2020

Leave Comment

Comments

Liked By