RadGridView - Hide rows when columns are not visible

1 Answer 111 Views
GridView
alex
Top achievements
Rank 2
Bronze
Iron
Iron
alex asked on 11 May 2023, 09:32 PM | edited on 11 May 2023, 09:37 PM

Hello,

I am working with a RadGridView and developed two features:

1. A ListBox that displays the RadGridView's columns where the user can check/uncheck some columns or all of them. The purpose of the control is to show or hide the columns according to the CheckBox state.

2. User Settings mechanism that saves the columns visibility status to a file. After the application starts, I load the IsVisible status to the columns and override the configured value in the xaml.

I noticed that the default behavior in RadGridView is to show the rows even when there are 0 visible columns and the SelectedItem remains.

I would like to hide the rows and unselect the items.

I tried to use a data trigger to do this funcionality.

It works with a DataTrigger when the value Columns.Count = 0.

To finish this functionality I would like to update the data trigger to the Visible Columns count, which is not provided by Telerik.

How can I do that?

I tried to create an attached property that maintains a counter when the user change the state of the columns visibility from the ListBox, but it does not work when the state is changed from the xaml or the user settings.

I don't want to bind to Columns Collection Changed and Visibility Changed because I think it is an overhead for this functionality. 

Can you expose this property or help me with a better solution?

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 16 May 2023, 01:21 PM

Hello Alex,

(1) To sync the visibility of the columns with a separate ListBox you can bind the IsVisible property of the columns to CheckBoxes in the ListBox items. Such approach is shown in the following articles:

(2) To save the visibility state of the columns, you can use the PersistenceFramework with a custom property provider. The following example shows how to implement custom property provider with RadGridView, but you will need only the part of the code that saves the columns. In other words, you can remove all the filtering, sorting and other settings.

An alternative solution here can be to manually save the visibility in your own json file and then restore it on start up.

(3) The GridView doesn't have a setting that will disable the rows generation when no columns are presented. To achieve this, you can manually remove the ItemsSource when all columns gets hidden or removed. You can listen for changes in the columns using the CollectionChanged event of the Columns collection and the PropertyChanged event of the columns. In case you need to re-use this behavior, you can introduce custom VisibleColumns count property. You can find this suggestion in the attached project. I hope this helps.

Regards,
Martin Ivanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

alex
Top achievements
Rank 2
Bronze
Iron
Iron
commented on 16 May 2023, 05:50 PM | edited

Thank you Martin for your detailed response.

I think that I was not clear.

I already implemented the option to hide columns eith a ListBox and I already save and read the columns visibility status using my own persistence framework (similar to yours).

The problems is that I want to set the item source to null when there are 0 visible columns and I don't have a simple way to get this value. I don't want to access the Persistencr Framework code while reading the file and then get the current visible columns.  Or counting during IsVisble chanhe of the ListBox. I want to avoid dependency between features which means that if someone change the columns visibility in code behingz then someone get notification and can set the source to null. In this case the style with a simple style trigger.

 

In my persistence framework I am notifief about columns add/removef to suscribe/remove Visibility Changed for each column. It works!

 

I don't want to do the same in another attached property for this feature. Every event has performance cost. Especially on my application where I have a lot of grids and columns (more than 50 per grid).

 

There is way to get the Columns Visibility Count with property changed?

Martin Ivanov
Telerik team
commented on 19 May 2023, 09:56 AM

The only API (that I found) that allows you to listen for changes in the columns visibility is the PropertyChanged event of the GridViewColumn elements. This idea is shown in the previously attached project. If that doesn't work and if you think that it will be useful for RadGridView to provide a built-in property or event that tells how many visible columns are there, you can post this request on the feedback portal.
alex
Top achievements
Rank 2
Bronze
Iron
Iron
commented on 02 Jun 2023, 04:04 PM | edited

Martin, you provide ColumnDisplayIndexChanged and ColumnWidthChanged. I don't think that it is a big effort to add ColumnVisibilityChanged. Can you open a ticket for that? Hopefully you add it to next versión:)
Martin Ivanov
Telerik team
commented on 06 Jun 2023, 06:28 AM

Hey Alex, I saw that you already opened a feature request about that. I am posting a link here so anyone else can find it if needed: https://feedback.telerik.com/wpf/1610459-gridview-add-visible-columns-count-api

 

Tags
GridView
Asked by
alex
Top achievements
Rank 2
Bronze
Iron
Iron
Answers by
Martin Ivanov
Telerik team
Share this question
or