c# - datatable as an itemsource for datagrid in wpf -
how can set datagrid control values datatable ?
i use source
public static readonly dependencyproperty objdatatabledefaultviewproperty = dependencyproperty.register("objdatatabledefaultview", typeof(system.data.dataview), typeof(window1), new frameworkpropertymetadata()); public system.data.dataview objdatatabledefaultview { { return (system.data.dataview)getvalue(objdatatabledefaultviewproperty); } set { setvalue(objdatatabledefaultviewproperty, value); } } private void createobjdatatable() { try { objdatatabledefaultview = table.defaultview; } catch (exception ex) { messagebox.show(ex.message); } }
but has got error message , program stops,
a first chance exception of type 'system.windows.markup.xamlparseexception' occurred in presentationframework.dll
additional information: cannot create instance of 'window1' defined in assembly 'readsky, version=1.0.0.0, culture=neutral, publickeytoken=null'. exception has been thrown target of invocation. error in markup file 'window1.xaml' line 1 position 9.
xaml <window x:class="readsky.window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="window1" height="296" width="738" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"> <grid> <my:datagrid autogeneratecolumns="false" margin="36,30,39,90" name="gridctrl" verticalalignment="stretch" horizontalalignment="stretch" isreadonly="true" rowbackground="gray" alternatingrowbackground="white" itemssource="{binding objdatatabledefaultview, elementname=uc, mode=oneway}"/> </grid> </window>
you try this:
<datagrid name="grid" autogeneratecolumns="true" itemssource="{binding}" /> public datatable tabledata { { datatable dt = new datatable(); dt.columns.add("col1"); dt.columns.add("col2"); dt.rows.add(new string[] {"val1", "val2"}); return dt; } } grid.datacontext = tabledata;
Comments
Post a Comment