csharp: Data binding in WPF DataGrid control

时间:2023-03-08 16:53:55
<Window x:Class="WpfProjectDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol">
<Grid>
<DataGrid AutoGenerateColumns="False" Height="59" HorizontalAlignment="Left" Margin="50,39,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="272" MinRowHeight="20" MinHeight="10" Loaded="dataGrid1_Loaded">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=id}" Header="序号" FontWeight="Bold"/>
<DataGridTextColumn Binding="{Binding Path=name}" Header="姓名"/> </DataGrid.Columns>
</DataGrid>
<DataGrid AutoGenerateColumns="False" Height="112" HorizontalAlignment="Left" Margin="47,148,0,0" Name="dataGrid2" VerticalAlignment="Top" Width="326" Loaded="dataGrid2_Loaded">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=ID}" Header="序号" FontWeight="Bold"/>
<DataGridTextColumn Binding="{Binding Path=Name}" Header="姓名"/>
<DataGridTextColumn Binding="{Binding Path=BookTitle}" Header="书藉名称" FontWeight="Bold"/>
<DataGridTextColumn Binding="{Binding Path=CreatDate}" Header="创作日期"/>
<DataGridTextColumn Binding="{Binding Path=IsMVP}" Header="是否MVP" FontWeight="Bold"/> </DataGrid.Columns> </DataGrid> </Grid>
</Window> using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data; namespace WpfProjectDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
/// <summary>
///
/// </summary>
/// <returns></returns>
DataTable setData()
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Rows.Add(1, "geovindu");
dt.Rows.Add(2, "涂聚文");
return dt;
} /// <summary>
///
/// </summary>
public MainWindow()
{
InitializeComponent(); } /// <summary>
/// List of Authors
/// </summary>
/// <returns></returns>
private List<Author> LoadCollectionData()
{
List<Author> authors = new List<Author>();
authors.Add(new Author()
{
ID = 101,
Name = "Geovin Du",
BookTitle = "Graphics Programming with GDI+",
CreatDate = new DateTime(1975, 2, 23),
IsMVP = false
});
authors.Add(new Author()
{
ID = 201,
Name = "涂聚文",
BookTitle = "Programming C#",
CreatDate = new DateTime(1982, 4, 12),
IsMVP = true
});
authors.Add(new Author()
{
ID = 244,
Name = "塗聚文",
BookTitle = "LINQ in Vista",
CreatDate = new DateTime(1985, 9, 11),
IsMVP = true
});
return authors;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGrid1_Loaded(object sender, RoutedEventArgs e)
{
// var grid = sender as DataGrid;
// grid.ItemsSource = LoadCollectionData();
//this.dataGrid1.ItemsSource = LoadCollectionData();
this.dataGrid1.ItemsSource = setData().DefaultView;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGrid2_Loaded(object sender, RoutedEventArgs e)
{
this.dataGrid2.ItemsSource = LoadCollectionData();
} } public class Author
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime CreatDate { get; set; }
public string BookTitle { get; set; }
public bool IsMVP { get; set; }
}
}