sorting values in combobox by typing

Go To StackoverFlow.com

0

I have a combobox that is filled with data field JobCode from database. There are 1000s of jobcode and when the user needs to select one jobcode he has to scroll down through all the jobcodes in the combobox. Can I do it in such a way that if the user types some letter of jobcode it will show the jobcodes which start with that letter in the combobox at the top of list so the user can select easily. For example, like adding some code in keypressevent in combobox.

The user must still choose from jobcodes in the list, not keep partially or incorrectly entered data that will cause wrong data entry at insert and update time.

public void jobcomboboxload()
{
       OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
       oleDbConnection1.Open();
       OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select jobpk,jobcode from  jobcodemastertable", oleDbConnection1);
       OleDbDataReader reader = oleDbCommand1.ExecuteReader();
       DataTable dt = new DataTable();
       dt.Columns.Add("jobpk", typeof(int));
       dt.Columns.Add("jobcode", typeof(string));
       dt.Load(reader);
       cmbjobcode.ValueMember = "jobpk";
       cmbjobcode.DisplayMember = "jobcode";
       cmbjobcode.DataSource = dt.DefaultView;

       oleDbConnection1.Close();
   }

jobcode is an unique field.

2012-04-05 21:10
by Sreenath Ganga
sreenath:) Such phrases like this as you mentioned"pls help pleaseeeeeeee" are not well-accepted on this site. So, don't mix them with your question in the future - Lion 2012-04-05 21:16
srry will take car - Sreenath Ganga 2012-09-02 09:42


1

  • Use cmbjobcode.AutoCompleteMode = AutoCompleteMode.Suggest (or other values of the enum)
  • Use cmbjobcode.AutoCompleteSource = AutoCompleteSource.ListItems
  • Change your query including the clause ORDER BY on the field jobcode

Please, don't forget the using statement around your OleDbConnection, OleDbCommand and OleDbDataReader. This will assure the proper dispose of the before mentioned variables.

For the checking on incomplete values, you should add the Validating event and, in that event, check if the text entered is present in your strings.
The combobox has a method called FindStringExact() that can help.

2012-04-05 21:18
by Steve
how do you do those dots - phadaphunk 2012-04-05 21:28
Select the line (one at time) and then press button Numbered List or the Button List over the answer bo - Steve 2012-04-05 21:33
Is one of teh formating options when you are entering / editing a post on here. above the text box on the right - Tony Hopkinson 2012-04-05 21:33
@steve thanks a lo - phadaphunk 2012-04-05 21:36


1

Set your ComboBox AutoCompleteMode properties to Suggest AND AutoCompleteSource to ListItems or else you won't see the suggestion.

Like Steve said you can change your query and add ORDER BY on your request field to set the order in which you want them in your SELECT statement.

Hope this helps don't hesitate if you have any questions.

2012-04-05 21:25
by phadaphunk
Ads