Question on SearchHelper

Hi,

i have 2 questions on SearchHelper:

1. Can it be used in a MVVM Viewmodel? i.e. without using Code behind

2. Is there also a Replace function, which would allow me to replace the found string? couldn't find one

thanks,

Helmut

5 Replies

SS Susmitha Sundar Syncfusion Team March 2, 2020 01:02 PM UTC

Hi Helmut, 
 
Thank you for using Syncfusion controls. 
 
You can use the SearchHelper with MVVM pattern (without code behind). Please refer the below sample link, 
 

We don’t have a built-in support for Replace function. But we have a wort around for this. Please refer the below code, 

void OnReplaceButton_Click(object sender, RoutedEventArgs e) 
{ 
     var item = this.ComboBox.SelectedItem; 
     if (item == null) 
         return; 
     var grid = this.GetDataGrid(item.ToString()); 
     var searchText = this.SearchTextBox.Text.ToUpper(); 
     foreach (RecordEntry record in grid.View.Records) 
     { 
         if ((record.Data as OrderInfo).CustomerID.Contains(searchText)) 
         { 
             (record.Data as OrderInfo).CustomerID = (record.Data as OrderInfo).CustomerID.Replace(searchText, this.ReplaceTextBox.Text); 
         } 
     } 
 
     grid.SearchHelper.Search(ReplaceTextBox.Text); 
     this.SearchTextBox.Clear(); 
     this.ReplaceTextBox.Clear(); 
     grid.View.Refresh();  
 } 


Please check the sample and let us know if you need further assistance on this. 

Regards, 
Susmitha S 



HW Helmut Wahrmann March 4, 2020 05:39 PM UTC

Hi Susmitha,

thanks for the answer. But with your solution i would need to loop through my collection and do a replace on all fields.
I don't need a searchhelper functionality for that. I could do that on my own.
I had a look at searchhelper and found that the "Findnext" updates CurrentRowColumnIndex.

With that and having a look at the "Edit" section for SfDatagrid, i came up with:

            grid.SearchHelper.FindNext(SearchTextBox.Text);
            var index = grid.SearchHelper.CurrentRowColumnIndex;
            if (index.RowIndex > -1)
            {
                var recordIndex = grid.ResolveToRecordIndex(index.RowIndex);
                var columnIndex = grid.ResolveToGridVisibleColumnIndex(index.ColumnIndex);
                var mappingName = grid.Columns[columnIndex].MappingName;
                var record = grid.View.Records.GetItemAt(recordIndex);
                var cellValue = grid.View.GetPropertyAccessProvider().GetValue(record, mappingName);
                var replacedString = (cellValue as string).Replace(SearchTextBox.Text, ReplaceTextBox.Text);
                grid.View.GetPropertyAccessProvider().SetValue(record, mappingName, replacedString);
            }

this works perfect.

thanks,

Helmut



SS Susmitha Sundar Syncfusion Team March 5, 2020 08:58 AM UTC

Hi Helmut, 
 
Thanks for the update. 
 
We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you. 
 
Regards, 
Susmitha S 



DT Dirk Tetenberg replied to Susmitha Sundar August 4, 2021 01:57 PM UTC

Hi Susmitha,

The link for the SearchHelper MVVM pattern example is dead. Can you please provide a new one? Many thanks.

Regards,

Dirk



MA Mohanram Anbukkarasu Syncfusion Team August 5, 2021 05:40 AM UTC

Hi Dirk, 

Thanks for the update.  

You can get the sample from the following link.  

You can also check all our DataGrid demo in a single project from the below given github link. 


Regards, 
Mohanram A. 


Loader.
Up arrow icon