1、绑定数据:dataGrid1.ItemsSource = dataSet.Tables[0].DefaultView;

注意:在创建DataGrid 时可以通过AutoGenerateColumns 属性设置列是否自动生成,从而加入自定义列。如果DataGrid 中同时包含“自动生成列”与“用户自定义列”,则首先创建“用户自定义列”。DataGrid 支持的四种列及其数据类型见下表:

DataGrid绑定列名、数据列代码:

 1            <DataGrid.Columns>
2 <DataGridTextColumn Header="客户编码" Binding="{Binding CustomerCode}"/>
3 <DataGridTextColumn Header="客户名称" Binding="{Binding CustomerName}"/>
4 <DataGridTextColumn Header="证件号码" Binding="{Binding CardID}"/>
5 <DataGridTextColumn Header="固定电话" Binding="{Binding Phone}"/>
6 <DataGridTextColumn Header="移动电话" Binding="{Binding MobilePhone}"/>
7 <DataGridTextColumn Header="电子邮件" Binding="{Binding Email}"/>
8 <DataGridTextColumn Header="QQ" Binding="{Binding QQ}"/>
9 <DataGridTextColumn Header="联系地址" Binding="{Binding Address}"/>
10 </DataGrid.Columns>

简单的数据显示可以在sql语句中将字段名另存在(as)中文列名,直接在DataGrid中作为列名使用。

2、自动生成行号(关键是最后一句):

          private void Window_Loaded(object sender, RoutedEventArgs e)
{
LoadDataGrid();
} private void LoadDataGrid()
{
dataGrid1.ItemsSource = UserDAL.GetUserDataSet().Tables[].DefaultView;//读取用户并刷新datagrid
dataGrid1.LoadingRow += new EventHandler<DataGridRowEventArgs>(dataGrid_LoadingRow);
} public void dataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = e.Row.GetIndex() + ;
}