DataGrid in WPF

In this article, we will discuss the DataGrid Control In WPF. In this example, we fill the data in the DataGrid with the help of a XML file. For this, follow these steps:

Step 1: Download the WPF Toolkit from the following Link:

Step 2: Now we add the reference of this Toolkit in our WPf Project Like this:

Click on the Project Menu and then click on the Add Reference. The following window will be appear:


Now we add the reference from the window. The following code will be added in the .xaml page:

<Window x:Class="Binding_Data_In_WPF.Window1"
    Title="Window1" Height="300" Width="300"  xmlns:my1="">

Step 3: After that, we will add the XML file (Students.xml) to our project. This file is used to fill the data in the DataGrid.

<?xml version='1.0'?>

Step 4: After that, we write the following code in the .xaml page.


        <XmlDataProvider x:Key="StudentData" Source="C:\Users\dell\Desktop\Binding Data In WPF\Binding Data In WPF\Students.xml"

Step 5: Now we add the DataGrid in our Page. Since it is part of the WPF toolkit, we write the following code in it:

            DataContext="{StaticResource StudentData}"
            ItemsSource="{Binding XPath=Student}"
            AutoGenerateColumns="False" Height="Auto"
            Name="dataGrid1" Margin="0,25,0,0"
            VerticalAlignment="Top" HorizontalAlignment="Stretch">

                    Binding="{Binding XPath=Id}"/>

                    Binding="{Binding XPath=Name}"/>
                    Binding="{Binding XPath=Subject1}"/>
                    Binding="{Binding XPath=Subject2}" />
                    Binding="{Binding XPath=Subject3}" />
                    Binding="{Binding XPath=Subject4}" />
                    Binding="{Binding XPath=Subject5}" />




Note: Here we create the column on the basis of our XML file.

The output will be: