New to Telerik UI for WinForms? Download free 30-day trial

Use custom filtering to search in all columns of RadMultiColumnComboBox.

Product Version Product Author Last modified
2014.1.402 RadMultiColumnComboBox for WinForms Dimitar Karamfilov February 13, 2014

This example shows how you can search in all columns in RadMultiColumnComboBox, rather than filtering by just a single column.  The example uses the CustomFiltering event of the EditorElement to perform a custom search. In this case we will search the entire underlying grid for the text in the editor and if a match is found, we will mark the corresponding cell in a specific color.

First we can initialize the control. To do that we can populate it with some data, enable the filtering by setting the appropriate properties (AutoFilter, DisplayMember) and add a custom FilterDescriptor (without this the CustomFiltering event won't fire). Also we should subscribe to the CustomFiltering and KeyDown events:

private void Form1_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'nwindDataSet.Customers' table. You can move, or remove it, as needed.
    this.customersTableAdapter.Fill(this.nwindDataSet.Customers);

    this.radMultiColumnComboBox1.AutoFilter = true;
    this.radMultiColumnComboBox1.DisplayMember = "ContactName";
    radMultiColumnComboBox1.Text = "";

    FilterDescriptor filter = new FilterDescriptor();
    filter.PropertyName = this.radMultiColumnComboBox1.DisplayMember;
    filter.Operator = FilterOperator.Contains;
    this.radMultiColumnComboBox1.EditorControl.MasterTemplate.FilterDescriptors.Add(filter);
    radMultiColumnComboBox1.MultiColumnComboBoxElement.EditorControl.EnableCustomFiltering = true;

    radMultiColumnComboBox1.MultiColumnComboBoxElement.EditorControl.CustomFiltering += EditorControl_CustomFiltering;
    radMultiColumnComboBox1.KeyDown += radMultiColumnComboBox1_KeyDown;

}

Now we can implement the CustomFiltering event handler. In it we will use a string variable to store the searched text (note that the text is corrected depending on the AutoCompleteMode). Also if the user clears the text we need to make sure that all rows are visible and their styles are reset. Finally we can iterate trough each cell of the current row and if it contains the searched text we can change its style:

void EditorControl_CustomFiltering(object sender, Telerik.WinControls.UI.GridViewCustomFilteringEventArgs e)
{
    RadMultiColumnComboBoxElement element = radMultiColumnComboBox1.MultiColumnComboBoxElement;

    string textToSearch = radMultiColumnComboBox1.Text;
    if (AutoCompleteMode.Append == (element.AutoCompleteMode & AutoCompleteMode.Append))
    {
        if (element.SelectionLength > 0 && element.SelectionStart > 0)
        {
            textToSearch = radMultiColumnComboBox1.Text.Substring(0, element.SelectionStart);
        }
    }
    if (string.IsNullOrEmpty(textToSearch))
    {
        e.Visible = true;

        for (int i = 0; i < element.EditorControl.ColumnCount; i++)
        {
            e.Row.Cells[i].Style.Reset();

        }
        e.Row.InvalidateRow();
        return;
    }

    e.Visible = false;
    for (int i = 0; i < element.EditorControl.ColumnCount; i++)
    {
        string text = e.Row.Cells[i].Value.ToString();
        if (text.IndexOf(textToSearch, 0, StringComparison.InvariantCultureIgnoreCase) >= 0)
        {
            e.Visible = true;
            e.Row.Cells[i].Style.CustomizeFill = true;
            e.Row.Cells[i].Style.DrawFill = true;
            e.Row.Cells[i].Style.BackColor = Color.FromArgb(201, 252, 254);
        }
        else
        {
            e.Row.Cells[i].Style.Reset();

        }
    }
    e.Row.InvalidateRow();

}

Finally we can use the KeyDown event in order to select the corresponding row (the DisplayMember text will be displayed in the textbox) when the user presses the enter key:

void radMultiColumnComboBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == System.Windows.Forms.Keys.Enter)
    {
        if (this.radMultiColumnComboBox1.ValueMember != "")
        {
            radMultiColumnComboBox1.SelectedValue = radMultiColumnComboBox1.EditorControl.CurrentRow.Cells[radMultiColumnComboBox1.ValueMember].Value;
        }
        else
        {
            radMultiColumnComboBox1.SelectedValue = radMultiColumnComboBox1.EditorControl.CurrentRow.Cells[radMultiColumnComboBox1.DisplayMember].Value;
        }

        radMultiColumnComboBox1.Text = radMultiColumnComboBox1.EditorControl.CurrentRow.Cells[radMultiColumnComboBox1.DisplayMember].Value.ToString();
        radMultiColumnComboBox1.MultiColumnComboBoxElement.ClosePopup();
        radMultiColumnComboBox1.MultiColumnComboBoxElement.TextBoxElement.TextBoxItem.SelectAll();
    }

}

You can download a VB and C# project from the following link.

In this article