How to instantiate a Datagridview(in code behind)

2019-08-23 07:02发布

问题:

I am actually using a datagrid which is filled by a datatable to display my data(NOTE: These are dynamic data). Till now I was proceeding as below

  grid = new Microsoft.Windows.Controls.DataGrid();

My datatable as

 table = new System.Data.DataTable();

and I fill the datagrid with the datatable by doing

   grid.ItemsSource = table.DefaultView;

However Due to dificulties I am facing to delete a column etc of the datagrid,I want to move to Datagridview instead of the datagrid.

I tried

 grid = new Microsoft.Windows.Controls.DataGridview();  //WRONG

But it is aparently not the correct way.

How do I do this?

NOTE: Framework 3.5 / WPF.

回答1:

Do you want to use native Winforms DataGridView in WPF? If yes, you should use WindowFormsHost and place DataGriView inside it. BTW, did you tried rebinding the datagrid after removing the column from your bound table? I am very sure that rebinding or restting the 'ItemsSource' should work.



回答2:

see Embedding a Windows Form into a WPF application - if you want use WindowsForms controls.

But the right way is using MVVM http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial (if you use wpf)



回答3:

I am posting the solution that suited me for future visitors. I found the answer to ALL my problems on this post >>HERE:http://www.c-sharpcorner.com/uploadfile/mahesh/using-windows-forms-controls-in-wpf/