16 Answers, 1 is accepted
The whole logic and initialization are inside the GridViewSearchPanel and its DataContext - SearchPanelViewModel. What could be done to get the behavior you want is to display the search panel initially so that the whole initialization logic is triggered and then bind the Text property of the TextBox you want to the SearchText property of the view model. It could be something like:
this.clubsGrid.ShowSearchPanel = false;
var searchPanel = this.clubsGrid.ChildrenOfType<
GridViewSearchPanel
>().FirstOrDefault();
this.txtBox.SetBinding(TextBox.TextProperty, new Binding("SearchText") { Source = searchPanel.DataContext, Mode = BindingMode.TwoWay });
Please give it a try and let me know whether it corresponds to your requirements.
Regards,
Maya
Telerik

I would also like to know if there is a simple way to bind to the Search Panel TextBox. I have several grids within a tab control that I would like to filter using a value that is bound to a TextBox outside of the tab control.
Is this possible?
Thanks,
Kirk
A simple way to achieve the desired behavior would be to handle the TextBox's TextChanged event and update the search criteria there:
public
MainWindow()
{
InitializeComponent();
this
.txtBox.TextChanged += TxtBox_TextChanged;
}
private
void
TxtBox_TextChanged(
object
sender, System.Windows.Controls.TextChangedEventArgs e)
{
var searchPanels =
this
.ChildrenOfType<GridViewSearchPanel>();
foreach
(var searchPanel
in
searchPanels)
{
(searchPanel.DataContext
as
SearchViewModel).SearchText =
this
.txtBox.Text;
}
}
Would such an approach be suitable for your requirements?
Regards,
Dilyan Traykov
Telerik by Progress

That does not work directly on the binding data context, you get an exception:
NullReferenceException: Waarde kan niet null zijn. Parameternaam: source (Value can not be null. Parameter: source)
bij System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
bij Telerik.Windows.Controls.GridView.SearchPanel.SearchFilterDescriptorsManager.UpdateFilterDescriptors(List`1 searchValueInfos)
bij Telerik.Windows.Controls.GridView.SearchPanel.SearchViewModel.set_SearchText(String value)
bij [Name of app].View.MainWindow.<>c__DisplayClass60_0.<GeneralSearchBox_OnTextChanged>b__1() in [the source file]

It only works once you have shown the searchbox at least once. (Since then there is something bound to the datacontext).
Would you know how to fix that without directly binding? To the textfield?
Thanks,
Marcel
As stated by my colleague in a previous post, the whole logic and initialization are inside the GridViewSearchPanel and its DataContext - the SearchPanelViewModel.
So the only way to achieve the desired behavior is to display the search panel initially so that the whole initialization logic is triggered and hide it afterwards if you do not want it being shown.
Regards,
Dilyan Traykov
Telerik by Progress

Any news on this? I want to use our own UI and not something baked in. I don't want to show this UI initially. I would like a more cleaner solution. Can I construct the SearchViewModel?
I'm afraid there's no other way to initialize the view model other than to use reflection which is not recommended.
Please note that showing and immediately hiding the panel will also work in this case without this being obvious to the end user. Is there any specific reason you do not find such an approach applicable?
Regards,
Dilyan Traykov
Progress Telerik

Your solution works perfectly if there are only columns with string type data. I have datetime column as well and it is throwing exception for the same.
Message is - "The string was not recognized as a valid DateTime. There is an unknown word starting at index 0."
Stack trace is - at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
What should I do for these datetime column please help.
I am using your code
*************
var searchPanels = this.ChildrenOfType<GridViewSearchPanel>();
foreach (var _searchPanel in searchPanels)
{
(_searchPanel.DataContext as Telerik.Windows.Controls.GridView.SearchPanel.SearchViewModel).SearchText = this.SearchTextbox.Text;
}
********
Hello shrinath,
Thank you for the provided code snippet.
I created a small sample project with it, however, the search functionality does not result in any exceptions at my end.
Could you please try running the example and let me know how it differs from the project you've set up at your end? If possible, please modify the project so that the exception is observed and I will gladly try to suggest a possible solution. You can either upload the modified project to a storage provider of your choice or open a new support ticket and attach it there.
Thank you in advance for your cooperation on the matter.
Regards,
Dilyan Traykov
Progress Telerik

Thank you for replying back.

I have encountered one more issue with the approach. If we have string containing spaces as a search text, the outcome is all the words from search text are matched one by one and results are highlighted.
However if you need to find the whole search text as one match from grid it doesn't provide the desired output.
For example if search text is "011 of 2020" then all '011', 'of' and '2020' matches are highlighted, this way if "011 of 2020" is at the last row of grid and there thousands of rows then user will need to scroll to bottom.
Can we modify this behaviour to search text "starts with" instead of "contains". I have attached screens from our app where exact "011 of 2020" is at last row of grid. one.jpg is top half and two.jpg is bottom half.

private void UserControl_Loaded(object sender, RoutedEventArgs e)<
br
> {<
br
> var searchPanel = this.navAreaListGrid.ChildrenOfType<
GridViewSearchPanel
>().FirstOrDefault();<
br
> <
br
> if (searchPanel != null)<
br
> {<
br
> SearchTextbox.SetBinding(TextBox.TextProperty, new Binding("SearchText") { Source = searchPanel.DataContext, Mode = BindingMode.OneTime });<
br
> navAreaListGrid.ShowSearchPanel = false;<
br
> }<
br
> }<
br
><
br
> private void WarningListTextSearch(object sender, TextChangedEventArgs args)<
br
> {<
br
> GridViewSearchPanel _searchPanel = navAreaListGrid.ChildrenOfType<
GridViewSearchPanel
>().FirstOrDefault();<
br
> (_searchPanel.DataContext as Telerik.Windows.Controls.GridView.SearchPanel.SearchViewModel).SearchText = string.Format(@"""{0}""", SearchTextbox.Text); ;<
br
>}

01.
private
void
UserControl_Loaded(
object
sender, RoutedEventArgs e)
02.
{
03.
var searchPanel =
this
.radGrid.ChildrenOfType<GridViewSearchPanel>().FirstOrDefault();
04.
05.
if
(searchPanel !=
null
)
06.
{
07.
SearchTextbox.SetBinding(TextBox.TextProperty,
new
Binding(
"SearchText"
) { Source = searchPanel.DataContext, Mode = BindingMode.OneTime });
08.
radGrid.ShowSearchPanel =
false
;
09.
}
10.
}
11.
12.
private
void
WarningListTextSearch(
object
sender, TextChangedEventArgs args)
13.
{
14.
GridViewSearchPanel _searchPanel =
15.
radGrid.ChildrenOfType<GridViewSearchPanel>().FirstOrDefault();
16.
(_searchPanel.DataContext
as
17.
Telerik.Windows.Controls.GridView.SearchPanel.SearchViewModel).SearchText =
18.
string
.Format(@
""
"{0}"
""
, SearchTextbox.Text); ;
19.
}
Hi shree,
I'm glad to hear that you've found a suitable workaround and would like to thank you for sharing it with our community.
I just want to add that you can also use the MatchAllTerms SearchMode which should provide the same result.
Regards,
Dilyan Traykov
Progress Telerik
Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive, special prizes, and more, for FREE?! Register now for DevReach 2.0(20).