问题
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