Wednesday, March 10, 2010

Auto-complete feature in .net for winforms controls

Many a times it is required for us to use the auto-complete feature in the controls of the winforms .net application. This feature of auto-complete can be applied both to textbox and combobox alike. The code for it in c#.net is as follows:

public void AutoCompleteTextBox(string tableName, string fieldName, TextBox txtToAutoComp)
        {
            AutoCompleteStringCollection txtCollection = new AutoCompleteStringCollection();
            DataTable dtAutoComp = Dal.ExecuteSP("StoredProcedure", fieldName, tableName);
            if (dtAutoComp.Rows.Count >= 0)
            {
                for (int count = 0; count < dtAutoComp.Rows.Count; count++)
                {
                    txtCollection.Add(dtAutoComp.Rows[count][fieldName].ToString());
                }
            }
            txtToAutoComp.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txtToAutoComp.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtToAutoComp.AutoCompleteCustomSource = txtCollection;
        }
For implementing the code for combobox just replace the textbox variable with combobox variable. Everything else remains same.

3 comments:

  1. Thanks Jaivardhan for the code... it really helped me a lot. Now I can show the users what all data relating to their entry is already in the database.

    ReplyDelete
  2. get the word repair utility to fix data corruption issues in the files of specified format

    ReplyDelete
  3. Providing effective Website Development Dubai web design to improve the customers for websites. Business people develop websites for world wide business. Giving impressible graphics to website full fill the customer needs.

    ReplyDelete

Comments to this post

LinkWithin

Related Posts with Thumbnails