Category / Section
How to get the AutoComplete on matching part of any string in the ComboBoxAdv items?
5 mins read
AutoComplete enables the AutoComplete behavior in the ComboBoxAdv. The AutoComplete on matching any part of the string in the ComboBoxAdv items with its text input can be achieved by using its TextChanged event.
The following code example demonstrates the same.
C#
//Creates DataTable DataTable dataTable = new DataTable(); // Creates a DataView. DataView listDataView; bool isTextCleared = true; string rowFilterText = string.Empty; // Adds Columns. dataTable.Columns.Add("FirstName"); dataTable.Columns.Add("LastName"); dataTable.Columns.Add("occupation"); dataTable.Columns.Add("place"); // Creates a Data Set. DataSet dataSet = new DataSet(); dataSet.Tables.Add(dataTable); dataTable.Rows.Add(new string[] { "John", "Tina", "Doctor", "Alaska" }); dataTable.Rows.Add(new string[] { "Mary", "Anu", "Teacher", "China" }); dataTable.Rows.Add(new string[] { "Asha", "Roy", "Staff", "London" }); dataTable.Rows.Add(new string[] { "George", "Gaskin", "Nurse", "Germany" }); dataTable.Rows.Add(new string[] { "Sam", "Jens", "Engineer", "Russia" }); dataTable.Rows.Add(new string[] { "Ben", "Geo", "Developer", "India" }); // Creates a DataView. DataView dataView = new DataView(dataTable); // Sets DataSource. this.comboBoxAdv1.DataSource = dataView; // Sets DisplayMember. this.comboBoxAdv1.DisplayMember = "place"; //Disables the AutoComplete behavior this.comboBoxAdv1.AutoComplete = false; void comboBoxAdv1_TextChanged(object sender, EventArgs e) { text = this.comboBoxAdv1.Text; if (this.comboBoxAdv1.Text == string.Empty && !isTextCleared) { this.comboBoxAdv1.DataSource = dataTable; isTextCleared = true; } else if (this.comboBoxAdv1.Text != string.Empty && isTextCleared) { //Gets the filter text rowFilterText = "[" + dataTable.Columns[dataTable.Columns[this.comboBoxAdv1.DisplayMember].Ordinal].ColumnName + "]" + " LIKE '" + "%" + this.comboBoxAdv1.Text + "%'"; listDataView = new DataView(this.dataTable, rowFilterText, null, DataViewRowState.CurrentRows); if (listDataView.Count > 0) { //Gets the data source of the ComboBoxAdv this.comboBoxAdv1.DataSource = listDataView; this.comboBoxAdv1.SelectionStart = 0; this.comboBoxAdv1.SelectionLength = this.comboBoxAdv1.Text.Length; comboBoxAdv1.DroppedDown = true; return; } else { comboBoxAdv1.DroppedDown = false; comboBoxAdv1.SelectionStart = this.comboBoxAdv1.Text.Length; } } }
VB
'Creates DataTable Private dataTable As New DataTable() 'Creates a DataView. Private listDataView As DataView Private isTextCleared As Boolean = True Private rowFilterText As String = String.Empty 'Adds Columns. dataTable.Columns.Add("FirstName") dataTable.Columns.Add("LastName") dataTable.Columns.Add("occupation") dataTable.Columns.Add("place") 'Creates a Data Set. Dim dataSet As New DataSet() dataSet.Tables.Add(dataTable) dataTable.Rows.Add(New String() { "John", "Tina", "Doctor", "Alaska" }) dataTable.Rows.Add(New String() { "Mary", "Anu", "Teacher", "China" }) dataTable.Rows.Add(New String() { "Asha", "Roy", "Staff", "London" }) dataTable.Rows.Add(New String() { "George", "Gaskin", "Nurse", "Germany" }) dataTable.Rows.Add(New String() { "Sam", "Jens", "Engineer", "Russia" }) dataTable.Rows.Add(New String() { "Ben", "Geo", "Developer", "India" }) 'Creates a DataView. Dim dataView As New DataView(dataTable) 'Sets DataSource. Me.comboBoxAdv1.DataSource = dataView 'Sets DisplayMember. Me.comboBoxAdv1.DisplayMember = "place" 'Disables the AutoComplete behavior Me.comboBoxAdv1.AutoComplete = False Private Sub comboBoxAdv1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) text = Me.comboBoxAdv1.Text If Me.comboBoxAdv1.Text = String.Empty AndAlso (Not isTextCleared) Then Me.comboBoxAdv1.DataSource = dataTable isTextCleared = True ElseIf Me.comboBoxAdv1.Text <> String.Empty AndAlso isTextCleared Then 'Gets the filter text rowFilterText = "[" & dataTable.Columns(dataTable.Columns(Me.comboBoxAdv1.DisplayMember).Ordinal).ColumnName & "]" & " LIKE '" & "%" & Me.comboBoxAdv1.Text & "%'" listDataView = New DataView(Me.dataTable, rowFilterText, Nothing, DataViewRowState.CurrentRows) If listDataView.Count > 0 Then 'Gets the data source of the ComboBoxAdv Me.comboBoxAdv1.DataSource = listDataView Me.comboBoxAdv1.SelectionStart = 0 Me.comboBoxAdv1.SelectionLength = Me.comboBoxAdv1.Text.Length comboBoxAdv1.DroppedDown = True Return Else comboBoxAdv1.DroppedDown = False comboBoxAdv1.SelectionStart = Me.comboBoxAdv1.Text.Length End If End If End Sub
Before selection:
After selection:
Sample Links:
C#: http://www.syncfusion.com/downloads/support/directtrac/129875/ze/ComboBoxAdv_FilterText1162825556
VB: http://www.syncfusion.com/downloads/support/directtrac/129875/ze/ComboBoxAdv_FilterText_VB1576498211