SIGN UP MEMBER LOGIN:    
ARTICLE

Dynamically Fill AutoComplete Textbox from DataGridView

Posted by Tajwer Jalal Articles | Current Affairs September 17, 2010
I wanted to write code for looking up records in DataGridView for which I decided use a simple auto-completing TextBox for easy search and dynamically refilled AutoCompleteStringCollection.
Reader Level:
Download Files:
 


Introduction

I wanted to write code for looking up records in DataGridView for which I decided to use a simple auto-completing TextBox for easy search and dynamically refilled AutoCompleteStringCollection. This task can be accomplished through ComboBox, for which I had to make a roundtrip to the database server for the data that I have already gotten in the girdview. So I decided to use grid data for filling AutoCompleteStringCollection.

Any feedback is welcome as your feedback will improve quality.

Background

I need to write codes to search records in DataGridView control. So I gave different fields on which search can be made, and I also want to avoid roundtrip to database server for filling ComboBox.

Using the Codes

I add a panel control for serving as find form (which remain hidden until find buttons are clicked), and places two command buttons cmdFind and cmdCancelFind along with a ComboBox cmbFindFields which is filled with field names and a TextBox named txtCustomerFind.

Added following codes in the Validated Event of cmbFindFields.

private void cmbFindFields_Validated(object sender, EventArgs e)
{

 AutoCompleteStringCollection acCustomer = new 
 AutoCompleteStringCollection();
 foreach (DataGridViewRow row in dbDetail.Rows)
  {
    if (cmbFindFields.SelectedItem.ToString() == "Customer ID")
       {
                    txtCustomerFind.MaxLength = 5;
                    acCustomer.Add(row.Cells[0].Value.ToString());
       }
       else if (cmbFindFields.SelectedItem.ToString() == "Customer
                      Name")

        {
                    acCustomer.Add(row.Cells[1].Value.ToString());
                    txtCustomerFind.MaxLength = 40;
         }
        else
         {

                    acCustomer.Add(row.Cells[2].Value.ToString());
                    txtCustomerFind.MaxLength = 40;
           }

            } txtCustomerFind.Clear();
 txtCustomerFind.AutoCompleteMode = AutoCompleteMode.Suggest;
 txtCustomerFind.AutoCompleteSource = AutoCompleteSource.CustomSource;
 txtCustomerFind.AutoCompleteCustomSource = acCustomer;

 }

Whenever user selects a field AutoCompleteSource of txtCustomerFind is changed respectively.

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

nice and simple code sir...

Posted by Sudhakar Chaudhary Apr 01, 2012

great coding sir

Posted by prasanna shah Nov 23, 2010
Nevron Gauge for SharePoint
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.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor