Add Windows GridView control in WPF dynamically in C#.

The purpose this article is to add Windows Data Grid View control to the WPF application with Windows Host object. From that build Data Grid View with inline code then place to Windows host object within C# code.

This article will explore  how to invoke Windows Data Grid View control into the Windows Presentation Foundation,because  there is no  standard control like Windows Data Grid View in WPF.To this we need to follow a few steps to achieve our goal.

Step 1:
     You need create a Window Presentation Foundation(WPF) Application  using visual studio. give name as WindowsDGVInWPF. look followings figure to get clear.

Look fine now we  look at the Window which has created by visual studio.The next step here we need add Window control within the WPF application, for this Microsoft released a library called as "WindowsFormHost".

Lets add the control within the your WPF application.this library existing within the .NET Framework 3.0 folder,just right click on the Project then select Add Refference menu.then move to  browse tab in the window.then navigate to the .NET Framework 3.0 folder and select the WindowsFormsIntegration.dll.look following figure to more.

finally you need add System.Windows.Form library also.Now everything fine to reference adding.

Lets write few XAML code in the Design window  to add Windows Form Host object.

You  need write following code to add Windows Form Host object into the window.

XAML File code

<Window x:Class="WindowsDGVInWPF.Window1"
Title="Window1" Height="300" Width="300">
<WindowsFormsHost Height="400" Margin="0,10,0,10" Name="windowsFormsHostDetail" Grid.ColumnSpan="2" Grid.Row="1"    VerticalAlignment="Top" >


Now start to build your Data Grid View by this just navigate to code file to the window1.xaml .

Just add this lines of code.

Code File

System.Windows.Forms.DataGridView dataGridViewDetail = null;

internal void WindowsFormGridInit()
System.Windows.Forms.DataGridViewTextBoxColumn Column2;
System.Windows.Forms.DataGridViewCheckBoxColumn Column3;
System.Windows.Forms.DataGridViewCheckBoxColumn Column4;
System.Windows.Forms.DataGridViewTextBoxColumn Column5;
System.Windows.Forms.DataGridViewCheckBoxColumn Column6;
System.Windows.Forms.DataGridViewTextBoxColumn columnDestValueField;
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();

dataGridViewDetail = new System.Windows.Forms.DataGridView();
dataGridViewDetail.AllowUserToAddRows = false;          
Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
columnDestValueField = new DataGridViewTextBoxColumn();
Column3 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
Column4 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
Column5 = new System.Windows.Forms.DataGridViewTextBoxColumn();
Column6 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
Column7 = new System.Windows.Forms.DataGridViewComboBoxColumn();


// dataGridView1
dataGridViewCellStyle1.BackColor = System.Drawing.Color.LemonChiffon;
dataGridViewCellStyle1.ForeColor = System.Drawing.Color.Black;
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.Color.Beige;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
dataGridViewDetail.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
dataGridViewDetail.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridViewDetail.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {

dataGridViewDetail.Location = new System.Drawing.Point(40, 46);
dataGridViewDetail.Name = "dataGridView1";
dataGridViewDetail.RowHeadersVisible = false;
dataGridViewDetail.Size = new System.Drawing.Size(655, 150);
dataGridViewDetail.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
dataGridViewDetail.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.Raised;
dataGridViewDetail.BackgroundColor = System.Drawing.Color.Gainsboro;
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
dataGridViewCellStyle2.ForeColor = System.Drawing.Color.Black;
dataGridViewCellStyle2.SelectionBackColor = System.Drawing.Color.Blue;
dataGridViewCellStyle2.SelectionForeColor = System.Drawing.Color.White;

dataGridViewDetail.TabIndex = 0;
// Column2
Column2.HeaderText = "Destination Display Name";
Column2.Name = "Column2";
Column2.DataPropertyName = "DestinationDisplayField";
Column2.ReadOnly = true;
Column2.Width = 200;
Column2.DefaultCellStyle = dataGridViewCellStyle2;
Column2.DisplayIndex = 0;

// Destionation Value Field

columnDestValueField.HeaderText = "Destination Element Name";
columnDestValueField.Width = 150;
columnDestValueField.DefaultCellStyle = dataGridViewCellStyle2;
columnDestValueField.DataPropertyName = "DestionationValueField";
columnDestValueField.ReadOnly = true;
columnDestValueField.Width = 200;
columnDestValueField.Name = "DestValueField";
columnDestValueField.DisplayIndex = 1;
// Column3
Column3.HeaderText = "Ignore";
Column3.DataPropertyName = "IsIgnore";
Column3.Name = "Column3";
Column3.DisplayIndex = 2;
Column3.Visible = false;
//Column3.DefaultCellStyle = dataGridViewCellStyle2;
// Column4
Column4.HeaderText = "Custom";
Column4.DataPropertyName = "IsCustom";
Column4.Name = "Column4";
Column4.DisplayIndex = 3;
Column4.Visible = false;
//Column4.DefaultCellStyle = dataGridViewCellStyle2;
// Column5
Column5.HeaderText = "Values";
Column5.DataPropertyName = "CustomValue";
Column5.Name = "Column5";
Column5.DefaultCellStyle = dataGridViewCellStyle2;
columnDestValueField.DisplayIndex = 4;
Column5.Visible = false;
// Column6
Column6.HeaderText = "Selection";
Column6.DataPropertyName = "IsFixed";
Column6.Name = "Column6";
Column6.DisplayIndex = 5;
Column6.Visible = false;
//Column6.DefaultCellStyle = dataGridViewCellStyle2;
// Column7

Column7.HeaderText = "CSV Source Fields";
Column7.DisplayMember = "DisplayValue";
Column7.ValueMember = "ValueMember";
Column7.DefaultCellStyle = dataGridViewCellStyle2;
Column7.DisplayIndex = 6;
Column7.Width = 200;
Column7.Name = "Column7";
windowsFormsHostDetail.Child = dataGridViewDetail;


This is internal method to build grid and add as child control to the WindowsFormHost just highlighted that important line.

Then we need call this method within the your window constructor. that;s like be followings lines of code.

Code File
public Window1()

Finally you need to bind data collection to the Grid View Control and also DropDownlist.

private void BindDataToGrid()
if (colectionDetailsContanier != null)
if (colectionDetailsContanier.Count > 0)

Column7.DataSource = colectionDetailsContanier[0].CollectionDataDefSrcFieldList;
Column7.MaxDropDownItems = 10;
Column7.AutoComplete = true;
Column7.Selected = true;
dataGridViewDetail.DataSource = colectionDetailsContanier;

Just call this function within the Window Load event like

Code File

void Window1_Loaded(object sender, RoutedEventArgs e)

Here colectionDetailsContanier is data source which has build up from the database records.everything look fine build the application and run you will get result like.

Note:Here  i set some of the data column visible false.

I hope this article help to who are trying to building DataGridView with WPF.if you have any questions or find any mistake please made as comments.

Thank you


By Ravenet Rasaiyah   Popularity  (10228 Views)