SIGN UP MEMBER LOGIN:    
ARTICLE

AutoComplete ComboBox in DataGridView using C#.net Windows Application

Posted by Shrikant S Articles | C# Language March 21, 2011
In this article you will learn How to use an AutoComplete ComboBox in DataGridView in C#.Net Windows Application
Reader Level:
Download Files:
 


Here is a quick example of how to use an AutoComplete ComboBox in DataGridView in a Windows Application.

  1. Create one Windows Application and add DataGridView from toolbox to design.
  2. Now create two DataGridViewComboBoxColumns and add them to the DataGridView:

public void ComboList1()
        {
            DataGridViewComboBoxColumn combo1 = new DataGridViewComboBoxColumn();
            combo1.HeaderText = "Country";
            combo1.Items.Add("Antarctica");
            combo1.Items.Add("Belgium");
            combo1.Items.Add("Canada");
            combo1.Items.Add("Finland");
            combo1.Items.Add("Albania");
            combo1.Items.Add("India");
            combo1.Items.Add("Barbados");
            dataGridView1.Columns.Add(combo1);
        }

public void ComboList2()
        {
            DataGridViewComboBoxColumn combo2 = new DataGridViewComboBoxColumn();
            combo2.HeaderText = "Types of Jobs";
            combo2.Items.Add("Accounting");
            combo2.Items.Add("HR");
            combo2.Items.Add("Finance");
            combo2.Items.Add("Transportation");
            combo2.Items.Add("Testing");
            dataGridView1.Columns.Add(combo2);
        }

Call both these methods from the Form Constructor.

Now Click on DataGridView and generate EditingControlShowing event and write the folllowing code in it:

if (e.Control is DataGridViewComboBoxEditingControl)
            {
                ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
                ((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems;
                ((ComboBox)e.Control).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
            }


This will work for all comboBoxes which are present in the DataGridView.

textbox1.gif

The AutoCompleteMode and AutoCompleteSource properties create a ComboBox that automatically completes input strings by comparing the prefix being entered to the prefixes of all strings in a maintained source. This is useful for ComboBox controls in which URLs, addresses, file names, or commands will be frequently entered. If there are duplicate entries in the maintained source, automatic completion behaves unpredictably.

Login to add your contents and source code to this article
share this article :
post comment
 

thanks

Posted by jayakrishnan Morayur May 02, 2012

but i wan to get value from one table and display in datagridview combobox column plz help me

Posted by faizan ahmad Apr 16, 2012

thanks boss

Posted by dattaram garud Jan 14, 2012

actually i have datagridview which already bind data and want to combo box in particular cell as user enter on cell in (C#.net) please help me lakhanp22@gmail.com Regards Lakhan Anjana

Posted by lakhan aanjana Dec 02, 2011

Its really vry gud code..it saves my time also reduce my coding length its in vb If TypeOf e.Control Is DataGridViewComboBoxEditingControl Then DirectCast(e.Control, ComboBox).DropDownStyle = ComboBoxStyle.DropDown DirectCast(e.Control, ComboBox).AutoCompleteSource = AutoCompleteSource.ListItems DirectCast(e.Control, ComboBox).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend End If

Posted by bhrugesh thakkar Oct 11, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor