Mastering WPF DataGrid in a Day: Hour 5 DataGrid Columns

Before reading this article, I highly recommend reading the previous parts:

AutoGenerateColumns

The AutoGenerateColumns property determines whether the DataGrid columns will be autogenerated at run-time based on the data source object and its properties. Each public property of the data source collection becomes a column in the DataGrid. The DataGrid control is smart enough to know what type of column to generate for various data types. As you saw in Figure 4, the DataGrid generated 5 columns for 5 public properties of the Author class and a CheckBox column was generated for a book type IsMVP property.

Note: The AutoGenerateColumns value is set to true by default for a DataGrid.

The XAML code snippet in Listing 7 sets the AutoGenerateColumns to True.

  1. <DataGrid HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"   
  2. Height="219" Width="465" Name="McDataGrid" AutoGenerateColumns="True"/>  
Listing 22

If the data source bound to the DataGrid has 50 columns and the user has set the AutoGenerateColumns property to “true” then it will generate and show all 50 columns for you. If you want to show fewer columns then set it to false and create the columns manually. We will discuss how to create columns manually later in this book.

ColumnWidth

The DataGrid control is smart enough to adjust and manage the width of its colummns. The ColumnWidth property represents the width settings of a DataGrid columns. The ColumnWidth is a DataGridLength structure that has the following key values that apply to columns: 
  • Auto: Automatic sizing mode.
  • DesiredValue: Calculated pixel value needed for columns.
  • DisplayValue: Pixel value needed for columns.
  • SizeToCells: Width based on the content of the cells.
  • SizeToHeader: Width based on the header.
  • Value: Width in pixels.

Let's look at the output of some of these values.

Figure 8 is the result of ColumnWidth="SizeToHeader".



Figure 8

Figure 9 is the result of ColumnWidth="SizeToCells".



Figure 9

Figure 10 is the result of ColumnWidth="100".



Figure 10

Figure 11 is the result of ColumnWidth="*".



Figure 11

CanUserResizeColumns

By default, the DataGrid allows users to resize its columns and rows. But you can restrict users if they can resize the DataGrid columns and rows.

The CanUserResizeRows property is used to set whether or not users can resize the row. The CanUserResizeColumns property is used to set whether or not users can resize the columns.

The code snippet in Listing 13 restricts users from resizing the rows and columns of the DataGrid.

  1. <DataGrid HorizontalAlignment="Left" Margin="4,5,0,0" VerticalAlignment="Top"   
  2. Height="120" Width="510" Name="McDataGrid" AutoGenerateColumns="True"  
  3. AlternatingRowBackground="LightGreen" GridLinesVisibility="Horizontal"  
  4. ColumnWidth="*" SelectionUnit="Cell" SelectionMode="Extended"  
  5. CanUserResizeColumns="False" CanUserResizeRows="False"/>  
Listing 23

CanUserSortColumns and CanUserReorderColumns

By default, the DataGrid allows users to sort and reorder its columns But you can restrict users from sorting or reordering the DataGrid columns.

The CanUserSortColumns property is used to set whether or not users can sort the columns. The CanUserReorderColumns property is used to set whether or not users can reorder the columns.

The code snippet in Listing 14 restricts users from sorting and reordering the columns of the DataGrid.
  1. <DataGrid HorizontalAlignment="Left" Margin="4,5,0,0" VerticalAlignment="Top"   
  2. Height="120" Width="510" Name="McDataGrid" AutoGenerateColumns="True"  
  3. AlternatingRowBackground="LightGreen" GridLinesVisibility="Horizontal"  
  4. ColumnWidth="*" SelectionUnit="Cell" SelectionMode="Extended"  
  5. CanUserSortColumns="False" CanUserReorderColumns="False"/>  
Listing 24

Most of the preceding properties you have seen earlier in this article may be set dynamically in code-behind. The Listing 25 sets these properties to false at run-time.
  1. McDataGrid.CanUserReorderColumns = false;  
  2. McDataGrid.CanUserResizeColumns = false;  
  3. McDataGrid.CanUserSortColumns = false;  
Listing 25

Summary

In this article of the Mastering WPF DataGrid series, we saw some of the basic DataGrid columns properties and how to use them.

The next article of this series will provide an in-depth coverage of the DataGrid columns.

 

<<Previous Article                                                Next Article>>

Up Next
    Ebook Download
    View all
    Learn
    View all