Csharp: Data binding in WPF DataGrid control, csharpwpf
<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 "verticalignment =" Top "Width =" 272 "MinRowHeight =" 20 "MinHeight =" 10 "Loaded =" dataGrid1_Loaded "> <DataGrid. columns> <maid Binding = "{Binding Path = id}" Header = "no." FontWeight = "Bold"/> <maid Binding = "{Binding Path = name}" Header = "name"/> </DataGrid. columns> </DataGrid> <DataGrid AutoGenerateColumns = "False" Height = "112" HorizontalAlignment = "Left" Margin = "47,148, 0, 0 "Name =" dataGrid2 "verticalignment =" Top "Width =" 326 "Loaded =" dataGrid2_Loaded "> <DataGrid. columns> <maid Binding = "{Binding Path = ID}" Header = "no." FontWeight = "Bold"/> <maid Binding = "{Binding Path = Name}" Header = "name"/> <maid Binding = "{Binding Path = BookTitle}" Header = "document name" FontWeight = "Bold"/> <maid Binding = "{Binding Path = CreatDate} "Header =" creation date "/> <maid 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. interaction logic of 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, "tu juwen"); 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 = "tu juwen", 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 maid (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 ;}}}