How to instantiate a Datagridview(in code behind)

帅比萌擦擦* 提交于 2019-12-12 00:27:25

问题


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/



来源:https://stackoverflow.com/questions/20537916/how-to-instantiate-a-datagridviewin-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!