DataGrid 简单数据绑定实例1

1.默认数据显示(自动显示列)

后台绑定

            //DataGrid 数据绑定
dataGridOne.ItemsSource = _Context.Info.ToList();

前台定义

        <DataGrid Name="dataGridOne"
HorizontalAlignment="Left" Margin="10,92,0,0"
VerticalAlignment="Top" Height="219"
Width="642"/>

结果显示:

DataGrid 简单数据绑定实例1

2.自定义显示列,DataGrid.AutoGenerateColumns 只是是否产生自动列

后台绑定同上

前台定义:

        <DataGrid  Grid.Row="1" AutoGenerateColumns="False"
x:Name="dataGridOne" Margin="20,20,20,20" >
<DataGrid.Columns>
<DataGridTextColumn Header="编号" Binding="{Binding Code}"/>
<DataGridTextColumn Header="姓名" Binding="{Binding Name}"/>
<DataGridTextColumn Header="性别" Binding="{Binding Sex}"/>
<DataGridTextColumn Header="民族" Binding="{Binding Nation}"/>
<DataGridTextColumn Header="生日" Binding="{Binding Birthday}"/>
</DataGrid.Columns>
</DataGrid>

结果显示:

DataGrid 简单数据绑定实例1

 3.自定义链接列显示

后台代码:

        private void Button_Click(object sender, RoutedEventArgs e)
{
//数据绑定
dataGridOne.ItemsSource = list;
}
public class UrlItem
{
public string Name { get; set; }
public int SortNumber { get; set; }
public string Url { get; set; }
}

前台定义:

        <DataGrid x:Name="dataGridOne"
AutoGenerateColumns="False"
AlternatingRowBackground="PaleGoldenrod"
ColumnWidth="100" HorizontalAlignment="Left" Margin="15,15,0,0"
Grid.Row="1" VerticalAlignment="Top" Height="173" Width="518"
CanUserAddRows="False"
>
<DataGrid.Columns>
<DataGridTextColumn Header="网站名称" Binding="{Binding Name}" IsReadOnly="True"/>
<DataGridTextColumn Header="网址" Binding="{Binding Url}"/>
<DataGridTextColumn Header="序号" Binding="{Binding SortNumber}"/>
<DataGridHyperlinkColumn Header="查看" Binding="{Binding Url}" Width="200"/>
</DataGrid.Columns>
</DataGrid>

显示结果:

DataGrid 简单数据绑定实例1

上一篇:Linux下用gSOAP开发Web Service服务端和客户端程序


下一篇:poj1195Mobile phones(二维树状数组)