Skip to content

SyncfusionExamples/How-to-Implement-an-AutoComplete-Cell-in-WinForms-DataGrid-SfDataGrid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

How to Implement an AutoComplete Cell in WinForms DataGrid (SfDataGrid)?

By default, Winforms DataGrid (SfDatagrid) does not provide built‑in AutoComplete functionality. However, you can achieve similar behavior by using a GridComboBoxColumn.

The AutoComplete behavior can be implemented by creating a custom GridComboBoxCellRenderer and overriding the OnInitializeEditElement method. Within this method, you can apply filtering to the dropdown items based on the characters typed in the cell by using the uiElement.DropDownListView.View.Filter property. And, you can update the row data based on the selected material within the SfDataGrid.CellComboBoxSelectionChanged event.

C#

//Code snippet for customizing the GridComboBoxCellRenderer

public class GridComboBoxCellRendererExt : GridComboBoxCellRenderer
{
    protected override void OnInitializeEditElement(
        DataColumnBase column, RowColumnIndex rowColumnIndex, SfComboBox uiElement)
    {
        base.OnInitializeEditElement(column, rowColumnIndex, uiElement);

        uiElement.DropDownButton.Visible = false;
        uiElement.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        if (uiElement.DropDownListView.View != null)
        {
            uiElement.DropDownListView.View.Filter = (object data) =>
            {
                if (data is not Material m) return false;
                var text = uiElement.Text ?? string.Empty;
                if (string.IsNullOrWhiteSpace(text)) return true;

                return m.Name?.IndexOf(text, StringComparison.OrdinalIgnoreCase) >= 0 || (m.Synonyms?.IndexOf(text, StringComparison.OrdinalIgnoreCase) >= 0);
            };
        }
    }

    protected override void OnUnwireEditUIElement(SfComboBox uiElement)
    {
        if (uiElement?.DropDownListView?.View != null)
        {
            uiElement.DropDownListView.View.Filter = null;
        }
        base.OnUnwireEditUIElement(uiElement);
    }
}


//Code snippet for update the row data

dataGrid.CellComboBoxSelectionChanged += dataGrid_CellComboBoxSelectionChanged;

private void dataGrid_CellComboBoxSelectionChanged(object sender, CellComboBoxSelectionChangedEventArgs e)
{
    if (e.GridColumn.MappingName == "MaterialID")
    {
        if (e.Record is LineItem li)
        {
            if (e.SelectedItem is Material selected)
                li.Material = selected;
        }
    }
}

LAutoComplete Cell

Take a moment to peruse the WinForms DataGrid - Columns documentation, to learn more about column and it's types with example.

About

This demo shows how to implement an AutoComplete cell in WinForms SfDataGrid.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages