WISE OWL EXERCISES
WPF - VISUAL C# EXERCISES
WPF - Visual C# | Datagrids exercise | Create, bind and format a simple datagrid
This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.
The answer to the exercise will be included and explained if you attend the course listed below!
You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
If necessary, run the SQL script in the above folder to generate the database of Top Trumps cards:
The table called tblCard whose CardTitle and Description fields we'll display.
Create a window called Geeks, and get it to show a DataGrid showing all of the cards whose PackId equals 1, using the default setting of autogenerating columns:
There is no shame - quite the opposite, in fact - in copying and pasting your code-behind to populate the data table of cards. You can then set the ItemsSource of the datagrid to bind to this table.
Now set AutoGenerateColumns to False, and add columns to your datagrid to show the card title and description:
The new version of the datagrid includes two columns only.
Now apply the following formatting and changes:
- Set the CanUserSortColumns property of the DataGrid and the SortMemberPath of each column so that sorting works (you could also set the SortDirection property of the card title column so that initially the geeks are sorted by their card title).
- Unset the CanUserAddRows property.
- Set a row background and alternating row background for the datagrid.
- Set suitable widths for each column.
You can see the effect of these changes on the next page.
This is how your datagrid might look after your formatting changes:
The datagrid with the above changes applied.
Finally, attach code to the MouseDoubleClick event of your datagrid so that you see the title of the chosen card as a message box title, and the description as the message:
What you would see if you were to double-click on Steve Jobs.
Close down this window!