How to implement searching in GridComboBoxColumn of WinForms DataGrid?
In WinForms DataGrid (SfDataGrid), partial search functionality in the GridComboBoxColumn can be implemented by overriding the GridComboBoxCellRenderer.
The OnInitializeEditElement method is overridden in the custom renderer. After invoking the base method, the TextChanged event of the WinForms ComboBox (SfComboBox) is hooked, as ComboBox acts as the edit element in the GridComboBoxColumn.
In the event handler, the ComboBox view is filtered based on the input text, using the Contains method to match partial entries.
// Remove the existing Renderer
sfDataGrid1.CellRenderers.Remove("ComboBox");
// Add the custom Renderer
sfDataGrid1.CellRenderers.Add("ComboBox", new GridCellComboBoxRendererExt());
//Renderer customization
public class GridCellComboBoxRendererExt : GridComboBoxCellRenderer
{
public GridCellComboBoxRendererExt() { }
protected override void OnInitializeEditElement(DataColumnBase column, RowColumnIndex rowColumnIndex, SfComboBox uiElement)
{
base.OnInitializeEditElement(column, rowColumnIndex, uiElement);
uiElement.TextChanged += UiElement_TextChanged;
}
string filteredText;
private void UiElement_TextChanged(object sender, EventArgs e)
{
var comboBox = sender as SfComboBox;
if (comboBox != null && comboBox.DropDownListView != null && comboBox.DropDownListView.View != null && comboBox.DropDownListView.View.Filter != null)
{
filteredText = comboBox.Text;
comboBox.DropDownListView.View.Filter = FilterItem;
comboBox.DropDownListView.View.RefreshFilter();
}
}
private bool FilterItem(object data)
{
if (data != null)
{
var orderInfo = data as OrderInfo;
if (orderInfo != null && filteredText != null && orderInfo.CustomerName.ToLower().Contains(filteredText.ToLower()))
return true;
}
return false;
}
}
Take a moment to peruse the WinForms DataGrid - Column Renderer documentation, to learn more about column renderer with examples.
Conclusion
I hope you enjoyed learning how to implement searching in gridcomboboxcolumn of WinForms DataGrid.
You can refer to our WinForms DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our WinForms DataGrid example to understand how to create and manipulate data.
For current customers, check out our components from the License and Downloads page. If you are new to Syncfusion, try our 30-day free trial to check out our other controls.
Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!