开发者

bind a WPF datagrid to a datatable

开发者 https://www.devze.com 2022-12-24 07:51 出处:网络
I have used the marvelous example posted at: http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx

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
    {
        privat开发者_如何学Ce 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()

edit: Repaired my Visual Studio. At least it gives me a better error message:

Request for the permission of type 'System.Data.OleDb.OleDBPermission,system.Data, Version=2.0.0.0, Culture=neutral,PublicKeyToken=b77a5c561934e089' failed.


The project I was developing was on a (corporate) network drive. If I move the project to my local C: drive the error goes away.

0

精彩评论

暂无评论...
验证码 换一张
取 消