bind a WPF datagrid to a datatable
- by Jim Thomas
I have used the marvelous example posted at:
http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx
to bind a WPF datagrid to a datatable.
The source code below compiles fine; it even runs and displays the contents of the InfoWork datatable in the wpf datagrid. Hooray! But the WPF page with the datagrid will not display in the designer. I get an incomprehensible error instead on my design page which is shown at the end of this posting. I assume the designer is having some difficulty instantiating the dataview for display in the grid. How can I fix that?
XAML Code:
xmlns:local="clr-namespace:InfoSeeker"
<Window.Resources>
<ObjectDataProvider
x:Key="InfoWorkData"
ObjectType="{x:Type local:InfoWorkData}" />
<ObjectDataProvider
x:Key="InfoWork"
ObjectInstance="{StaticResource InfoWorkData}"
MethodName="GetInfoWork" />
</Window.Resources>
<my:DataGrid
DataContext="{Binding Source={StaticResource InfoWork}}"
AutoGenerateColumns="True"
ItemsSource="{Binding}"
Name="dataGrid1"
xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" />
C# Code:
namespace InfoSeeker
{
public class InfoWorkData
{
private InfoTableAdapters.InfoWorkTableAdapter infoAdapter;
private Info infoDS;
public InfoWorkData()
{
infoDS = new Info();
infoAdapter = new InfoTableAdapters.InfoWorkTableAdapter();
infoAdapter.Fill(infoDS.InfoWork);
}
public DataView GetInfoWork()
{
return infoDS.InfoWork.DefaultView;
}
}
}
Error shown in place of the designer page which has the grid on it:
An unhandled exception has occurred:
Type 'MS.Internal.Permissions.UserInitiatedNavigationPermission' in Assembly 'PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.
at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
...At:Ms.Internal.Designer.DesignerPane.LoadDesignerView()