Search Combo Box like Google Search

不羁的心 提交于 2019-12-09 11:05:26

问题


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...


回答1:


    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";
    }



回答2:


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.




回答3:


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




回答4:


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.


来源:https://stackoverflow.com/questions/15878862/search-combo-box-like-google-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!