StartsWith search in RadGridView

Environment

Product Version Product Author
2019.2.618 RadGridView for WinForms Desislava Yordanova

Description

By default, RadGridView searches for cell values that 'Contain' the search criteria.

starts-with-search-in-radgridview001

This article demonstrates how to search only for the cells that 'StartsWith' the search criteria.

starts-with-search-in-radgridview002

Solution

All you need to do, is to create a custom GridViewSearchRowInfo. Subscribe to the CreateRowInfo event at design time and use the following code snippet:


private void radGridView1_CreateRowInfo(object sender, GridViewCreateRowInfoEventArgs e)
{
    if (e.RowInfo is GridViewSearchRowInfo)
    {
         e.RowInfo = new CustomSearchRow(e.ViewInfo);
    }
}

public class CustomSearchRow : GridViewSearchRowInfo
{
    public CustomSearchRow(GridViewInfo viewInfo) : base(viewInfo)
    {
    }

    protected override bool MatchesSearchCriteria(string searchCriteria, GridViewRowInfo row, GridViewColumn col)
    {
        return (row.Cells[col.Name].Value + "").StartsWith(searchCriteria); 
    }
}



 Private Sub RadGridView1_CreateRowInfo(sender As Object, e As Telerik.WinControls.UI.GridViewCreateRowInfoEventArgs) _
    Handles RadGridView1.CreateRowInfo
    If TypeOf e.RowInfo Is GridViewSearchRowInfo Then
        e.RowInfo = New CustomSearchRow(e.ViewInfo)
    End If
End Sub
Public Class CustomSearchRow
    Inherits GridViewSearchRowInfo

    Public Sub New(ByVal viewInfo As GridViewInfo)
        MyBase.New(viewInfo)
    End Sub

    Protected Overrides Function MatchesSearchCriteria(ByVal searchCriteria As String, ByVal row As GridViewRowInfo, _
                                                       ByVal col As GridViewColumn) As Boolean
        Return (row.Cells(col.Name).Value & "").StartsWith(searchCriteria)
    End Function
End Class


See Also

In this article
Not finding the help you need? Improve this article