Jaa


DataGridView in a WPF Application

 

In my previous blog, I talked about a new WindowsForms Control “ElementHost” to host WPF controls inside a WindowsForms Project. On the contrary, “WindowsFormsHost” is used to host winforms controls inside a WPF application. WindowsFormsHost is a very important control to get the rich capabilities of WinForms world into WPF. WPF does not have a story yet for some of the controls like DataGridView, ToolStrips etc which have lots of uses.

In this blog, I will concentrate on getting a DataGridView getting displayed inside a WPF application. I will make use of databinding to get data from the Customers Table of the SaveNorthWind Database.

 

The steps would be as follows

  1. Create a new WPF Application in C#
  2. Add reference to System.Windows.Forms.dll
  3. Add the “WindowsFormsHost” control from the toolbox onto the WPF window.
  4. From the Data Menu on the toolbar “Select Add New Data Source “ and connect to the Customers Table of SaveNorthWind database
    1. After successfully doing this step, you can see SaveNorthWindDataSet added to the application
  5. In the Window1.xaml.cs file, create the following function. (This basically does the plumbing work to get the data from the customers table in the form of a bindingsource object)

 

        BindingSource dataBindingPrep()

        {

            SaveNorthwindDataSet dataset = new SaveNorthwindDataSet();

            dataset.DataSetName = "SaveNorthwindDataSet";

            dataset.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;

            SaveNorthwindDataSetTableAdapters.CustomersTableAdapter tableadapter = new WpfApplication1.SaveNorthwindDataSetTableAdapters.CustomersTableAdapter();

            BindingSource bs = new BindingSource();

            bs.DataMember = "Customers";

            bs.DataSource = dataset;

            tableadapter.ClearBeforeFill = true;

            // filling the table

            tableadapter.Fill(dataset.Customers);

            return bs;

        }

 

  1. Create a DataGridView control, set its datasource property
  2. set the child property of windowsformshost to the datagridview

 

        public Window1()

        {

            InitializeComponent();

            //adding the datagridview

            DataGridView dgv = new DataGridView();

            dgv.DataSource = dataBindingPrep();

            //setting the child property

            windowsFormsHost1.Child = dgv;

            //hooking events to DGV

            dgv.CellContentClick += new DataGridViewCellEventHandler(dgv_CellContentClick);

        }

 

when you run your app, you can see the wpf window showing the data from the customers table in a datagridview.

 

You can also try to add events to the datagridview. In my example, I choose to show a messagebox when I click on the column1 of the DGV. I can do this by implementing the cellcontentclick event as follows.

 

        void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)

        {

            DataGridView dgv = (DataGridView)sender;

            if (e.ColumnIndex == 0)

            {

                System.Windows.MessageBox.Show("Clicked on " + dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);

            }

        }

Comments

  • Anonymous
    August 11, 2007
    Thanks for the post! Can't get it to work, though - data gets read from the database, but the grid remains empty (VS2008 Beta2) Regards

  • Anonymous
    May 24, 2008
    this post is very useful, can change

  1. Create a new WPF Application in C#   into 1.Create a new WPF Browser Application in C# ?? thanks a lot
  • Anonymous
    November 16, 2009
    Hi.. that was a very helping post for beginners like me. thanks

  • Anonymous
    January 21, 2015
    hi How to add columns in hosted winform datagridview in wpf? thanks]

  • Anonymous
    August 01, 2015
    The comment has been removed