How to instantiate a Datagridview(in code behind)

2019-08-23 06:21发布

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.

3条回答
2楼-- · 2019-08-23 06:57

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.

查看更多
孤傲高冷的网名
3楼-- · 2019-08-23 07:07

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)

查看更多
Fickle 薄情
4楼-- · 2019-08-23 07:13

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/

查看更多
登录 后发表回答