The RadGridView for WPF uses the ItemsSource property for binding data.  ItemsSource is actually of type System.Object which not only allows you to bind to any data source that implements IEnumerable, but also the ADO.NET DataTable and DataSet.  In this post, I am going to demonstrate how you can quickly bind data to the RadGridView for WPF.  The task will be to build a car repair shop application which will display all the cars currently in the shop.  To get started we need to have some data, so we will use a simple class to store Car information.



    public class Car  
        public string Make { getset; }  
        public string Model {getset; }  
        public DateTime ModelYear { getset; }  
        public int Doors { getset; }  
        public bool AllWheelDrive { getset; }  
        public string Complaint { getset; }  
        public string VehicleOwner { getset; }  


Now to keep the example simple we can create a routine to build a List of Car objects.  I called this routine GetCarsInTheShop and creates two new Car objects and add them to the List<Car> which is returned to the caller.  

public List<Car> GetCarsInTheShop()  
    var car1 = new Car()  
        AllWheelDrive = false,  
        Doors = 4,  
        Make = "Ford",  
        Model = "Taurus",  
        ModelYear = new DateTime(2004, 01, 01),  
        Complaint = "Won't start",  
        VehicleOwner = "Dudley Dooright" 
    var car2 = new Car()  
        AllWheelDrive = true,  
        Doors = 2,  
        Make = "Honda",  
        Model = "Accord",  
        ModelYear = new DateTime(2006, 01, 01),  
        Complaint = "Flat tire",  
        VehicleOwner = "Mary Smith" 
    var cars = new List<Car>()  
    return cars;  

Now that the preparation steps are out of the way, the next step is to drag and drop a new RadGridView onto the Window.  You will notice that the grid displays sample data during design time, but this will not be visible when you run the application.   

Design View
Runtime View

Finally, it is time to bind the data to the RadGridView in the constructor of the Window.  As you will see in the code below, I am simply assigning the resulting List<Car> from the GetCarsInTheShop routine to ItemsSource property.



public Window1()  
    //bind the list of cars to the grid  
    radGridView1.ItemsSource = GetCarsInTheShop();  

After I run the application and the data is displayed appropriately in the grid, as expected.  Not only that, but the appropriate editor for the cell datatype is displayed when you enter edit mode.


ModelYear (DateTime data type) displays DatePicker editor for cell.


AllWheelDrive (Boolean data type) displayed CheckBox editor for cell.


And there you have it, this shows just how simple it is to bind data to the RadGridView for WPF. 

Related Posts


Comments are disabled in preview mode.