We have a few datagrids that contain a lot of columns, and different columns are important to different users. They want to see only a subset of what is available, and use different fields as keys to the dataset. So, we had to come up with a way for the users to:
1. Rearrrange the columns
2. Make columns visibile/invisible
3. Freeze some columns in the beginning (keys) so they remain visible during horizontal scrolling.
4. Carry over the user preferences to future sessions.
We managed to come up with a generic user control to do this for any datagrid, and I think it is worth sharing here. Here is an application that demonstrates the capability:
It is just the same datagrid from previous posts, with some extraneous fields to play around with. You get the idea.
The code for the user control can be found here
To use the DataGridLayoutChooser, you would write some xaml like this:
The property AppKey is the key that it uses to save the preferences in isolated storage, for use in future sessions.
When "Loaded" event handler for your app/usercontrol, you want to specify the DataGrid that the Chooser is formatting:
InitLayout() applies any layout that the user may have saved from a previous session.
Now, when the user wants to manage the layout (Click event in the demo, for instance), you call the ShowLayoutDialog() method on the Chooser:
We will take a deeper look at the user-control code in the next post. The DataGridLayoutChooser is a very good candidate to be made into a real control, by the way. I will make that available in a few weeks, when I get around to it. In the meantime, I hope you find this UserControl as useful as we did.
1. Rearrrange the columns
2. Make columns visibile/invisible
3. Freeze some columns in the beginning (keys) so they remain visible during horizontal scrolling.
4. Carry over the user preferences to future sessions.
We managed to come up with a generic user control to do this for any datagrid, and I think it is worth sharing here. Here is an application that demonstrates the capability:
It is just the same datagrid from previous posts, with some extraneous fields to play around with. You get the idea.
The code for the user control can be found here
To use the DataGridLayoutChooser, you would write some xaml like this:
1: <lcl:DataGridLayoutChooserName="dgLayoutChooser"AppKey="dgLayout"Grid.RowSpan="2"/>
When "Loaded" event handler for your app/usercontrol, you want to specify the DataGrid that the Chooser is formatting:
1: void Home_Loaded(object sender, RoutedEventArgs e)
2: {
3: dgLayoutChooser.GridToFormat = dg;
4: dgLayoutChooser.InitLayout();
5: }
Now, when the user wants to manage the layout (Click event in the demo, for instance), you call the ShowLayoutDialog() method on the Chooser:
1: void ManageLayout(object sender, RoutedEventArgs e)
2: {
3: dgLayoutChooser.ShowLayoutDialog();
4: }