how to make a datagridviewcombobox as searchable combobox

i have a datagridview,in which first column includes a combo box.i use ms-access(database), table name is "item" . this combobox values are filled with itemid values. how to make that combobox as searchable combobox? in the sense, the user should be able to enter the item id in that combobox.
if he enters 'a', it should display itemids which starts with 'a' only(eg:a10,a11..).
LVL 7
Maverick_CoolAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gene_CypCommented:
1) Create a handler for the KeyPress event for your combobox. In your OnKeyPress handler be sure to exclude keys such as <Enter>

if (e.KeyChar != (Char)Keys.Return || e.KeyChar != (Char)Keys.Enter)
2) The List you binded to your combobox, has to be searched and a sub-list created (so that the original remains intact.
If your list is directly extracted from a Database you can do the following:
http://www.dotnetspider.com/forum/182283-search-combobox-items-by-alphabetically.aspx
 3) If your list is "within your program" and jsut binded to your Combobox, you can create a sublist based on the text you've entered. Given a list of strings.
4) You take that list and bind it to your combobox, have the default on your combobox to show as index 0 (check that the list.length > 0)
 
 

private List<string> GetRefinedList(List<string> fullList, string searchText)
        {
            List<string> subList = new List<string>();

            foreach (string str in fullList)
            {
                if (str.StartsWith(searchText))
                    subList.Add(searchText);
            }

            return subList;
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Maverick_CoolAuthor Commented:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            ComboBox combo = e.Control as ComboBox;
            if (combo != null)
            {
           
                combo.TextChanged -=
                   new EventHandler(ComboBox_TextChanged);
             
                combo.TextChanged +=
                    new EventHandler(ComboBox_TextChanged);
                combo.BindingContext = new BindingContext();
                combo.SelectedIndex = -1;
            }
        }

 public void ComboBox_TextChanged(object sender, EventArgs e)
        {
         
            ComboBox cmbBox = (ComboBox)sender;
                       
            if(myconnection.State==ConnectionState.Closed)
            myconnection.Open();
     
        if (cmbBox.SelectedIndex >0)
        {

            string commandString = "select a.ItemId as ItemCode, a.ItemName,a.Unit,a.BasePrice,a.discount,b.TaxId,b.TaxName,b.TaxPer from Item a left JOIN Tax b ON a.nTaxId = b.TaxId  WHERE a.ItemId='" + cmbBox.Text.ToString() + "'";
            OleDbCommand cmd = new OleDbCommand(commandString, myconnection);
            OleDbDataReader dr1 = cmd.ExecuteReader();
            dr1.Read();
            if (dr1.HasRows)
            {
   
                DataGridViewComboBoxCell colItm = (DataGridViewComboBoxCell)(dataGridView1.CurrentRow.Cells[0]);
           
                colItm.Value = dr1.GetValue(0).ToString();
                dataGridView1.CurrentRow.Cells[1].Value = dr1.GetValue(1).ToString();
                dataGridView1.CurrentRow.Cells[2].Value = dr1.GetValue(3).ToString();
                dataGridView1.CurrentRow.Cells[3].Value = dr1.GetValue(5).ToString();
                dataGridView1.CurrentRow.Cells[4].Value = dr1.GetValue(7).ToString();
                dataGridView1.CurrentRow.Cells[5].Value = dr1.GetValue(6).ToString();
                dataGridView1.CurrentRow.Cells[6].Value = 0;
                dataGridView1.CurrentRow.Cells[7].Value = dr1.GetValue(2).ToString();
                dataGridView1.CurrentRow.Cells[8].Value = dr1.GetValue(4).ToString();
                dataGridView1.CurrentRow.Cells[9].Value = 0 * float.Parse(dr1.GetValue(4).ToString());
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
               
            }
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].Name == "bPrice" || dataGridView1.Columns[e.ColumnIndex].Name == "qty" || dataGridView1.Columns[e.ColumnIndex].Name == "Discount" || dataGridView1.Columns[e.ColumnIndex].Name == "itemid")
            {
                dataGridView1.BeginEdit(true);

            }            
        }
i have included this code in my application.  i  made  datagridview  combobox editable. whenever  key  "tu" is pressed  it will displays  item ids  which starts  with  "tu"  along with remaining  item ids. problem is , user  cannot  see  what he has  entered.  i need to make  user  key pressed  value  visible. and  also,  it should display  only the  values  starts with  "tu"  in the drop down list.
0
Gene_CypCommented:
Maverick, did you notice the code I gave you for creating a "sub list" ? Have you tried putting it to use?
0
Maverick_CoolAuthor Commented:
actually i didnt want to make use of lists and i tried that but it wont works 4 me sorry
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.