
- When the user unchecks the toggle the search should clear and the user will still be able to type the searchbar but it will not execute any search
- when the user checks the toggle if there is a text in the searchbar, the search will be executed based on that text and the user will be able to seach from now on
I tried this:
private string searchText { get; set; }
private void MainDatagrid_Searching(object sender, GridViewSearchingEventArgs e)
{
if (!(bool)showFilteredToggleButton.IsChecked)
{
e.Cancel = true;
searchText = e.SearchText;
}
else
{
e.Cancel = false;
searchText = e.SearchText;
}
}
private void showFilteredToggleButton_Checked(object sender, RoutedEventArgs e)
{
if (!(bool)showFilteredToggleButton.IsChecked)
{
var clearSearchValue = GridViewSearchPanelCommands.ClearSearchValue as RoutedUICommand;
clearSearchValue.Execute(null, MainDatagrid.ChildrenOfType<GridViewSearchPanel>().FirstOrDefault());
}
else
{
var searchBytextCommand = RadGridViewCommands.SearchByText as RoutedUICommand;
searchBytextCommand.Execute(searchText, MainDatagrid);
}
}
But there are some features missing such as when user checks the toggle the search isn't initiated and when the user unchecks the toggle the searchbar clears itself but the search isn't cleared, old search remains. How can achieve the functionality I described? Can you help me?
1 Answer, 1 is accepted
Hello Özgür,
I have added a sample project that shows this.
I hope this helps. Should you have any other questions do not hesitate to ask.
Regards,
Dimitar
Progress Telerik
This went so much smoother than I thought, I really appreciate it man, thanks. I have 3 further questions which are minor. I hope you can help me with those as well.
1. How can I preserve the search text in the search bar when the filtering is not happening.
2. How can I preserve the state of a selected row when the filtering clears.
3. How can I make the my datagrid's specific column start sorted in the direction I want. (I tried SortingState="Ascending" IsCustomSortingEnabled="True" but no luck)
I would be really thankful if you can fulfill my needs about these 3 situations. Thanks in advance.
Hi Özgür,
I am glad that this works for your case.
1. I am not sure what happens here. Can you give me an example of this?
2. Please note that the search does not select any rows. If you manually select a row it will remain selected, but you need to focus on the grid:
private void showFilteredToggleButton_Unchecked(object sender, RoutedEventArgs e)
{
this.clearSearch = true;
var clearSearchValue = GridViewSearchPanelCommands.ClearSearchValue as RoutedUICommand;
clearSearchValue.Execute(null, radGridView.ChildrenOfType<GridViewSearchPanel>().FirstOrDefault());
radGridView.Focus();
}
3. You need to add a sort descriptor. Here is an example that will work with the sample project:
<telerik:RadGridView.SortDescriptors>
<telerik:ColumnSortDescriptor Column="{Binding Columns[Name], ElementName=radGridView}" SortDirection="Descending"/>
</telerik:RadGridView.SortDescriptors>
You need to remove the custom sorting as this is a different functionality.
Off-topic, I can see that you have a license, and if you need faster answers, please submit a support ticket.
I am looking forward to your reply.
1. I meant when I uncheck the toggle button the search filter clears itself thats great, when I check it the filter comes back on, thats great as well however when I uncheck the toggle I can not see the text I have searched before. I know this sounds pretty greedy but the client wants it that way so if you can help me with that I'd be appreciated.
2. This does not work unfortunately I mean the row stays selected and when I click up-down arrows I can navigate to that row's section but it does not automatically navigate just by focusing.
3. This worked perfectly fine thank you.
Hi Özgür,
1. YOu can use the following approach for this:
private void showFilteredToggleButton_Unchecked(object sender, RoutedEventArgs e)
{
string text = this.searchText;
this.clearSearch = true;
var clearSearchValue = GridViewSearchPanelCommands.ClearSearchValue as RoutedUICommand;
clearSearchValue.Execute(null, radGridView.ChildrenOfType<GridViewSearchPanel>().FirstOrDefault());
this.Dispatcher.Invoke(() =>
{
var panel = radGridView.ChildrenOfType<GridViewSearchPanel>().FirstOrDefault();
if (panel != null)
{
var textbox = panel.ChildrenOfType<TextBox>().FirstOrDefault((TextBox c) => c.Name == "PART_SearchAsYouTypeTextBox");
textbox.Text = text;
}
});
radGridView.Focus();
}
2. Can you send me a video showing what is happening in this case?
You can use one of the approaches here to scroll to a particular row: WPF DataGrid - Scroll to Particular Row or Column - Telerik UI for WPF.
Hi Dimitar,
1. Worked perfectly. Tried without invoking and still worked would it be a problem? And I want to know if I can make the search highlighted column to appear in the screen since sometimes the search might be in the columns that is not in the view at that moment. I will be appreciated if you could help me about these. These are the last questions about this one, promise...
2. I couldn't attach video since it won't let me upload .mp4 file. Is there a way that I can send the screen capture to you?
Hi Özgür,
You need to archive it first only zip, rar and image file types are allowed.
Hi Özgür,
I have attached an updated version of the project that shows this. It seems that the search is clearing the selected row and this is why you need to restore it at a later point. I have used the ScrollIntoViewAsync method as well.
Let me know how this work for you.
This now preserves the selected item in the view thanks to you. However it still does not horizontally scroll if the searched text is out of view (under another column). Since this is not that important you don't have to look at this but if you have an idea I would like to hear it.
I have a much bigger problem right now after unchecking the filter toggle when I type 1 or 2 letters it searches those letters then doesn't search the rest of the word. After that when I check it it does not search at all. I have to add or subtract the letters in order it to activate the search again. Underneath I will be sharing my codes related with the gridview and I want you to look at it and notice me if there is a problem that might cause the issue I mentioned. And a general glimpse would be appreciated as well.
{
if (MainDatagrid.SelectedItem != null && MainDatagrid.SelectedItem != selectedItem)
{
selectedItem = MainDatagrid.SelectedItem as Log;
}
}
private void radGridView_Searching(object sender, GridViewSearchingEventArgs e)
{
if (!(bool)showFilteredToggleButton.IsChecked)
{
if (!clearSearch)
{
e.Cancel = true;
searchText = e.SearchText;
}
}
else
{
e.Cancel = false;
searchText = e.SearchText;
}
clearSearch = false;
if (e.SearchText == "" || !(bool)showFilteredToggleButton.IsChecked)
{
MainDatagrid.ScrollIntoViewAsync(selectedItem, null);
MainDatagrid.SelectedItem = selectedItem;
}
}
private void showFilteredToggleButton_Checked(object sender, RoutedEventArgs e)
{
RoutedUICommand? searchBytextCommand = RadGridViewCommands.SearchByText as RoutedUICommand;
string searchTextCache = searchText;
searchBytextCommand.Execute(string.Empty, MainDatagrid);
searchBytextCommand.Execute(searchTextCache, MainDatagrid);
MainDatagrid.ScrollIntoViewAsync(selectedItem, null);
MainDatagrid.SelectedItem = selectedItem;
_ = MainDatagrid.Focus();
}
private void showFilteredToggleButton_Unchecked(object sender, RoutedEventArgs e)
{
string text = searchText;
clearSearch = true;
RoutedUICommand? clearSearchValue = GridViewSearchPanelCommands.ClearSearchValue as RoutedUICommand;
clearSearchValue.Execute(null, MainDatagrid.ChildrenOfType<GridViewSearchPanel>().FirstOrDefault());
GridViewSearchPanel? panel = MainDatagrid.ChildrenOfType<GridViewSearchPanel>().FirstOrDefault();
if (panel != null)
{
TextBox? textbox = panel.ChildrenOfType<TextBox>().FirstOrDefault();
textbox.Text = text;
}
MainDatagrid.ScrollIntoViewAsync(selectedItem, null);
MainDatagrid.SelectedItem = selectedItem;
_ = MainDatagrid.Focus();
}
private void clearAllFiltersButton_Click(object sender, RoutedEventArgs e)
{
// Clear all the applied filters for every column
MainDatagrid.FilterDescriptors.SuspendNotifications();
foreach (GridViewDataColumn column in MainDatagrid.Columns.Cast<GridViewDataColumn>())
{
column.ClearFilters();
}
MainDatagrid.FilterDescriptors.ResumeNotifications();
MainDatagrid.ScrollIntoViewAsync(selectedItem, null);
MainDatagrid.SelectedItem = selectedItem;
}
Özgür,
1. Here is what you can use for the horizontal offset:
var scrollViewer = this.radGridView.FindChildByType<GridViewScrollViewer>();
var startOffset = scrollViewer.HorizontalOffset;
//your code
scrollViewer.ScrollToHorizontalOffset(startOffset);
2. I have examined the code and I am not sure what is causing this. The best approach for such cases would be to reproduce this in a project and send it to me for investigation. You can use my test project or open a new support ticket and send your actual project (the tickets are private threads).
I am looking forward to your reply.
Telerik 2024.1.130
.Net 6.0
telerik:AnimationManager.IsAnimationEnabled="False" but no luck with the expand animation, with collapse animation it works perfectly fine, colapses instantly but in the expand animation nothing changes.
Hi Özgür,
I am not sure how to reproduce the exception. I have attached a git that shows what I am doing. Could you please check it and let me know what I am missing?
As to the animations at hand, I want to ask you to open a new thread(ticket or forum). I am asking since the provided information is not enough for me to say why this is not working. Please include the exact control type, specify the theme that you are using, and if there are any customizations there.
I am looking forward to your reply.