Posted by: Zeeshan Amjad | November 23, 2009

Simple Usage of Data Grid Control in WPF 4


WPF 4.0 introduced lots of new feature and one of them is Data Grid control. Here we are going to study the simple usage of Data grid control. Here is a class diagram of DataGrid.

DataGrid

Here is a XAML code of this project.

  1: <Window x:Class="grid.MainWindow"
  2:         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:         Title="Example of Data Grid" Height="300" Width="300">
  5:     <Grid>
  6:         <DataGrid Name="grid" AlternationCount="2" Background="AliceBlue" AlternatingRowBackground="LightGreen">
  7:         </DataGrid>
  8:     </Grid>
  9: </Window>
 10: 

We just create a list of our user define class and assign it to the grid class. Here is a complete C# code of this project.

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Text;
  5: using System.Windows;
  6: using System.Windows.Controls;
  7: using System.Windows.Data;
  8: using System.Windows.Documents;
  9: using System.Windows.Input;
 10: using System.Windows.Media;
 11: using System.Windows.Media.Imaging;
 12: using System.Windows.Navigation;
 13: using System.Windows.Shapes;
 14: 
 15: namespace grid
 16: {
 17:     /// <summary>
 18:     /// Interaction logic for MainWindow.xaml
 19:     /// </summary>
 20:     public partial class MainWindow : Window
 21:     {
 22:         private List<Student> student;
 23: 
 24:         public MainWindow()
 25:         {
 26:             InitializeComponent();
 27: 
 28:             student = new List<Student>();
 29:             student.Add(new Student(10, "Bob", "Smith"));
 30:             student.Add(new Student(25, "James", "Brown"));
 31:             student.Add(new Student(15, "Joe", "Martin"));
 32:             student.Add(new Student(12, "Dona", "Taylor"));
 33:             student.Add(new Student(18, "Peter", "Brian"));
 34: 
 35:             grid.ItemsSource = student;
 36:         }
 37:     }
 38: 
 39:     public class Student
 40:     {
 41:         public Student(int id, String firstName, String lastName)
 42:         {
 43:             ID = id;
 44:             FirstName = firstName;
 45:             LastName = lastName;
 46:         }
 47: 
 48:         public int ID
 49:         { get; set; }
 50: 
 51:         public String FirstName
 52:         { get; set; }
 53: 
 54:         public String LastName
 55:         { get; set; }
 56:     }
 57: }
 58: 

Here is the output of this project.

DataGridOutput

Advertisements

Responses

  1. what steps to display customers of norhwind database n where to place northwind database?

    • First thank to visit my blog. You have to get data from database into object using either ADO.Net, ORM (Object Relational Mapping), Entity Framework, Native APIs or any other technique. Once you have data in your class (or Model) then it is no different than using data getting from Web Service, File System or even hard coded values in the program. You might have to create one database interaction layer to get data from database.

      Thanks and best Regards
      Zeeshan Amjad


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: