GridView Disable Search via Toggle Button

1 Answer 19 Views
GridView
Özgür Efekan
Top achievements
Rank 1
Özgür Efekan asked on 26 Mar 2024, 01:58 PM
I am using telerik's RadGridView as my datagrid and I want to make the search disabled when a toggle button is switched, the things I want are like so:
- 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

Sort by
0
Accepted
Dimitar
Telerik team
answered on 27 Mar 2024, 08:09 AM

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

A brand new ThemeBuilder course was just added to the Virtual Classroom. The training course was designed to help you get started with ThemeBuilder for styling Telerik and Kendo UI components for your applications. You can check it out at https://learn.telerik.com
Özgür Efekan
Top achievements
Rank 1
commented on 27 Mar 2024, 09:32 AM

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.

Dimitar
Telerik team
commented on 29 Mar 2024, 08:54 AM

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.

 

 

Özgür Efekan
Top achievements
Rank 1
commented on 29 Mar 2024, 12:19 PM

Thanks for your reply again. I couldn't find the support ticket that's why I submitted my issue as a question, I will do like that next time. As for the replies:

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.
Dimitar
Telerik team
commented on 01 Apr 2024, 09:48 AM

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

 

 

Özgür Efekan
Top achievements
Rank 1
commented on 01 Apr 2024, 11:22 AM

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?

Dimitar
Telerik team
commented on 01 Apr 2024, 01:03 PM

Hi Özgür, 

You need to archive it first only zip, rar and image file types are allowed. 

Özgür Efekan
Top achievements
Rank 1
commented on 02 Apr 2024, 07:25 AM

There you go.
Dimitar
Telerik team
commented on 02 Apr 2024, 11:15 AM

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.

Özgür Efekan
Top achievements
Rank 1
commented on 02 Apr 2024, 01:04 PM

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.

private void RadGridView_SelectedCellsChanged(object sender, GridViewSelectedCellsChangedEventArgs e)
{
    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;
}
Dimitar
Telerik team
commented on 04 Apr 2024, 09:14 AM

Ö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.

 

Özgür Efekan
Top achievements
Rank 1
commented on 04 Apr 2024, 09:53 AM

I did nothing to your test project but the issue persists on that demo as well. Does it have to do anything with my Telerik or .Net version?
Telerik 2024.1.130
.Net 6.0
Özgür Efekan
Top achievements
Rank 1
commented on 04 Apr 2024, 10:00 AM

I couldn't get the first step to work but it is not that important.
Özgür Efekan
Top achievements
Rank 1
commented on 04 Apr 2024, 11:41 AM

I have one more question which is not related to these topics at all. I couldn't manage to disable the expander's expand animation no matter what I've tried. I have used this:
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.
Dimitar
Telerik team
commented on 05 Apr 2024, 07:00 AM

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.

Özgür Efekan
Top achievements
Rank 1
commented on 16 Apr 2024, 06:38 AM

I have opened several tickets regarding these issues. I was at vacation so I couldn't respond, sorry for that. Thank you for your help throughout. I will persue from the tickets from now on. Take care :)
Tags
GridView
Asked by
Özgür Efekan
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or