Mention that the DataGrid is used frequently, both for Web pages and for application development. It allows us to flexibly display various data between rows and columns. This article will detail the related features of the DataGrid in WPF 4.
Custom Columns
By default, when we set the Itemsource property for the DataGrid control, the DataGrid automatically generates the corresponding columns based on the data type, and the following table lists the four columns supported by the DataGrid and their data types.
When you create a DataGrid, you can add custom columns by setting whether the columns are automatically generated by the AutoGenerateColumns property. If the DataGrid contains both auto-generated columns and user-defined columns, create user-defined columns first. These four columns are created separately in the following code instance:
<window x:class= "Wpf4controltest.mainwindow"
Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns:local= "Clr-namespace:wpf4controltest"
Xmlns:assembly= "Clr-namespace:system;assembly=mscorlib"
Title= "MainWindow" height= "width=" >
<Window.Resources>
<objectdataprovider x:key= "Sexenum" methodname= "GetValues"
Objecttype= "{x:type Assembly:enum}" >
<ObjectDataProvider.MethodParameters>
<x:type type= "Local:sexopt"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid>
<datagrid x:name= "DataGrid" itemssource= "{Binding}" autogeneratecolumns= "False" >
<DataGrid.Columns>
<datagridtextcolumn header= "name" width= "binding=" {Binding Name} "/>
<datagridtextcolumn header= "Age" width= "a" binding= "{Binding age}"/>
<datagridcomboboxcolumn width= "header=" Sex "
selecteditembinding= "{Binding Sex}"
Itemssource= ' {Binding source={staticresource sexenum} '/>
<datagridcheckboxcolumn header= "pass Exam?" Width= "100"
Binding= "{Binding pass}"/>
<datagridhyperlinkcolumn header= "Email" width= "150"
binding= "{Binding Email}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>