Pages

Friday 19 April 2013

WPF DataGrid Tutorials: DataGrid Properties and Features

WPF DataGrid Tutorials: DataGrid Properties and Features

WPF DataGrid has a lot of amazing features and properties which you can implement with ease in your application. WPF DataGrid provides a lot of properties which you can set to get your desired look and feel.

To show a basic WPF datagrid, just drop a DataGrid control to your view and bind the ItemsSource to a collection of data objects. The DataGrid provides a feature called AutoGenerateColumns that automatically generates column according to the public properties of your data objects.

<DataGrid ItemsSource="{Binding Customers}"  AutoGenerateColumns="False" />

The ItemSource property of DataGrid is the key to data binding. You can bind any data source that implements IEnuemerable. Each row in the DataGrid is bound to an object in the data source and each column in the DataGrid is bound to a property of the data source objects.

WPF DataGrid generates the following types of columns:

DataGridCheckBoxColumn for boolean values
DataGridComboBoxColumn for enumerable values
DataGridHyperlinkColumn for Uri values
DataGridTemplateColumn to show any types of data by defining your own cell template
DataGridTextColumn to show text values

WPF DataGrid Properties are listed below:

CanUserReorderColumns enables or disables column re-ordering
CanUserResizeColumns enables or disables column resizing
CanUserResizeRows enables or disables row resizing
CanUserSortColumns enables or disables column sorting

Grid Background, Row Background

<DataGrid ItemsSource="{Binding Customers}" Background="LightGray" RowBackground="LightYellow"/>

AlternatingRowBackground: You can define a an AlternatingRowBackground that is applied every even row. You can additionally specify an AlternationCount if you only want to ink every n-th data row.

<DataGrid ItemsSource="{Binding Customers}" AlternatingRowBackground="Gainsboro"  AlternationCount="2"/>

ColumnWidth and RowHeight properties of DataGrid are used to set the default column width and row height of DataGrid columns and rows.

GridLinesVisibility property is used to make grid lines visible. Using this option you can show and hide vertical, horizontal, all, or none lines.

SelectionMode can be set to Single or Extended to define if one or multiple units can be selected simultaneously.

SelectionUnit defines the scope of one selection unit. It can be set to Cell, CellAndRowHeader and FullRow.
 
BorderBrush and BorderThickness properties are used to set the color and width of the border.

IsReadOnly property is used to make a DataGrid read only. That means you cannot edit a DataGrid.

AreRowDetailsFrozen property is used to freeze the row details area so that it cannot be resized.

FrozenColumnCount: WPF datagrid supports the feature to freeze columns. That means they stay visible while you scoll horizontally through all columns. This is a useful feature to keep a referencing column like an ID or a name always visible to keep your orientation while scrolling.

<DataGrid ItemsSource="{Binding Customers}" FrozenColumnCount="2"  />

RowDetailsTemplate: WPF datagrid provides a feature that shows a detail panel for a selected row. It can be enabled by setting a DataTemplate to the RowDetailsTemplate property. The data template gets the object that is bound to this row passed by the DataContext and can bind to it.

HeadersVisibility: You can control the visibility of row and column headers by setting the HeadersVisibility property to either None,Row,Column or All.

<DataGrid ItemsSource="{Binding Customers}" HeadersVisibility="None" />

HorizontalScrollBarVisibility and VerticalScrollBarVisibility properties of type ScrollBarVisibility enumeration control the horizontal and vertical scrollbars of the DataGrid. It has four values - Auto, Disabled, Hidden, and Visible. The default value of these properties is Auto, that means, when scrolling is needed, you will see it, otherwise it will be hidden.

No comments:

Post a Comment

About the Author

I have more than 10 years of experience in IT industry. Linkedin Profile

I am currently messing up with neural networks in deep learning. I am learning Python, TensorFlow and Keras.

Author: I am an author of a book on deep learning.

Quiz: I run an online quiz on machine learning and deep learning.