9

I am making a Windows Form in that I have A combo box, Into which i have loaded some 'Invoice Numbers', from SQL server 2010. I want to Display Invoice Numbers as the User types into the Combo box. For eg if User types '100' then the Invoice Numbers Starting with '100' should be displayed in the dropdown.

Please Help, Thanks in Advance...

Sam
  • 736
  • 5
  • 15
  • 27
  • possible duplicate : check this http://stackoverflow.com/questions/11780558/c-sharp-winforms-combobox-dynamic-autocomplete – Chandru velan Apr 08 '13 at 12:36

4 Answers4

15
    DataTable temp;
    DataTable bank;
    private void Form1_Load(object sender, EventArgs e)
    {
        comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;

        temp = DbRdRw.SqlDbRead("Select * from BankMaster", "BankMaster");

        DataView dtview = new DataView(temp);
        dtview.Sort = "BankName DESC";
        bank = dtview.ToTable();

        comboBox1.DataSource = bank;
        comboBox1.ValueMember = "BankName";
        comboBox1.DisplayMember = "BankName";
    }
Sam
  • 736
  • 5
  • 15
  • 27
  • 1
    I tried this code and it worked... Thanks for all who tried answering my question... – Sam Apr 09 '13 at 07:48
8

Fill your combo box with items from database on loading then set Combo box properties:

AutoCompleteMode: Suggest Append

AutoCompleteSource: ListItems

Make sure to set the DropDown style to DropDown so user can type in. Just make a validations if inputted text on combo box does exist on the list before accepting.

hope it helps.

Drew Kruezer
  • 81
  • 1
  • 3
  • According to your answer I tried, but it not works like %name%. Then I changed this to key press that give me write first time then blcok the matching input. my code snippet https://ideone.com/L3LqNk – Kabir Hossain Oct 22 '18 at 06:52
1

Try AutoCompleteMode - either Suggest or SuggestAppend depending on the exact behavior you're looking for. Also, remember to set AutoCompleteSource for the list that AutoComplete will base auto completion from (I suggest ListItems).

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.autocompletemode.aspx

TimWagaman
  • 980
  • 1
  • 10
  • 31
1

What you need to do here is:

  • Get an event every time the user types a character in to the text box.
  • Have a function that runs on this event to read the contents of the box ('100' in your example) and fire a query off to your database, for example:

    SELECT InvoiceNumber from Invoices WHERE InvoiceNumber LIKE '100%'

  • Display the matching results back in a combo box for the user to select from.
snappieT
  • 544
  • 6
  • 16
  • isn't in MSSQL `*` instead of `%`? – EaterOfCode Apr 08 '13 at 12:39
  • 1
    I've used `%` with success in SQL Server 2010 at least – snappieT Apr 08 '13 at 12:40
  • I used your method, wrote the code in Textchanged event of Combo box Here is the Code string a = cbxBankName.Text + "%"; BankMasterTable = DbRdRw.SqlDbRead("Select * from BankMaster where BankName Like '" + a + "'", "BankMaster");//data retrived to a datatable //data loading starts cbxBankName.DataSource = BankMasterTable; cbxBankName.ValueMember = "BankName"; cbxBankName.DisplayMember = "BankName"; But it is Displaying only one value and not the similar values – Sam Apr 08 '13 at 13:08
  • Continuation of prev coment: if i type 'B' then it should display all Banks name starting with B like 'Bank of India' and 'Bank of Maharashtra' but it displays only 'Bank of India'. and on deleting also it displays it for ever. – Sam Apr 08 '13 at 13:13
  • Try running the SQL query directly against your database in SQL Server Management Studio and see what records it returns. You code looks OK, I don't see why it would only show one result. – snappieT Apr 08 '13 at 16:40