绑定数据
你可以将GridControl绑定到任何实现IEnumerable接口的对象或它的下级对象(例如,IList,ICollection)。要将控件绑定到数据上,你应该为网格的DataControlBase.ItemsSource属性分配一个数据源。
<Window xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" ...> <dxg:GridControl ItemsSource="{Binding Customers}" AutoGenerateColumns="AddNew"/> </Window>
在后台的数据绑定中,grid.ItemsSource = 数据集是通用的,只是在前端xml表达式上有明显区别。
将Grid绑定到MS Access数据库
<Window x:Class="DXGrid_BindingToMSAccessDb.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" Title="Window1" Height="291" Width="518"> <Grid> <dxg:GridControl x:Name="grid" AutoGenerateColumns="AddNew"> <dxg:GridControl.View> <dxg:TableView /> </dxg:GridControl.View> </dxg:GridControl> </Grid> </Window>将Grid绑定到一个XML文件
<Window x:Class="DXGrid_BindingToXML.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" Title="Window1"> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="30" /> </Grid.RowDefinitions> <dxg:GridControl x:Name="grid" VerticalAlignment="Top"> <dxg:GridControl.Columns> <dxg:GridColumn FieldName="FirstName" /> <dxg:GridColumn FieldName="LastName" /> <dxg:GridColumn FieldName="Company" /> <dxg:GridColumn FieldName="City" /> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TableView AutoWidth="True" /> </dxg:GridControl.View> </dxg:GridControl> <Button Margin="5" Grid.Row="1" Click="Button_Click" VerticalAlignment="Bottom" HorizontalAlignment="Left" Content="Post Data to an XML File"/> </Grid> </Window>显示非绑定数据
<Window x:Class="DXGrid_UnboundColumns.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" Title="Window1" Height="300" Width="552"> <Grid> <dxg:GridControl x:Name="grid" CustomUnboundColumnData="grid_CustomUnboundColumnData"> <dxg:GridControl.Columns> <dxg:GridColumn FieldName="ProductName" /> <dxg:GridColumn FieldName="UnitPrice"> <dxg:GridColumn.EditSettings> <dxe:TextEditSettings DisplayFormat="c2" /> </dxg:GridColumn.EditSettings> </dxg:GridColumn> <dxg:GridColumn FieldName="UnitsOnOrder" /> <dxg:GridColumn FieldName="Total" UnboundType="Boolean" ReadOnly="True"> <dxg:GridColumn.EditSettings> <dxe:TextEditSettings DisplayFormat="c2" /> </dxg:GridColumn.EditSettings> </dxg:GridColumn> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TableView AutoWidth="True"/> </dxg:GridControl.View> </dxg:GridControl> </Grid> </Window>