如果内容绑定到Datatable,如何动态删除WPF Datagrid列名? [英] How to delete WPF Datagrid column name Dynamically if content is bound to Datatable?
问题描述
我的Datagrid已绑定到数据表.但它显示了一个额外的列.我想删除该列.
当我尝试在数据表中获取datagrid的内容时,当我尝试将Datagrid的内容获取到数据表中时,出现无法将类型System.windows.Controls.Datagrid转换为System.Data.Datatable"的错误. br/>
正确的方法是什么?
我的代码:DataTable dataTable =(DataTable)dataGrid;
My Datagrid is bound to datatable. But it displays an extra column. I want to delete that column.
when i am trying to get the content of datagrid in datatable I am Getting an Error as "cannot convert type System.windows.Controls.Datagrid to System.Data.Datatable" when i try to get the content of Datagrid into a datatable.
What is the correct way to do it?
My Code: DataTable dataTable = (DataTable)dataGrid;
推荐答案
您发布的代码试图从DataGrid创建DataTable.除非您执行类似的操作
,否则这是不可能的
The code you posted is trying to create a DataTable from your DataGrid. This is not possible unless you do something like
DataTable dt = (DataTable)dataGrid.DataContext;
但即使如此,它仍无法正确计算.如果要从绑定的DataGrid中删除列,则需要从基础绑定源中删除该字段.
希望这对您有帮助
but even still this does not really compute properly. If you want to delete columns from a bound DataGrid then you need to delete the field from the underlying Binding Source.
Hope this helps
Thanx Wayne.但是你能告诉我如何从底层的绑定源中删除吗???
Thanx Wayne. But can u tell me how to delete from the underlying Binding Source????
这篇关于如果内容绑定到Datatable,如何动态删除WPF Datagrid列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!