Databinding array of custom classes to DataGrid

Discussion in 'ASP .Net Datagrid Control' started by Joe Rattz, Apr 22, 2004.

  1. Joe Rattz

    Joe Rattz Guest

    I can't seem to bind an array of custom classes to my DataGrid. I get the following error

    "A field or property with the name 'dcAbbreviation' was not found on the selected datasource:

    This seems like it should be simple as falling off a log, but I can't find it. I also can't seem to find any useful code or article on the web either. Here are my specifics

    Here's my class I have an array of

    [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:types:WEJOEI:genpt.com")
    public class messageData_t

    /// <remarks/
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)
    public string dcAbbreviation

    /// <remarks/
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)
    public string messageNumber

    /// <remarks/
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)
    public string messageText


    You may recognize it as a generated web service proxy class...so don't ask me to change it please

    Here's my DataGrid Columns section

    <ASP:BOUNDCOLUMN HeaderText="DC" DataField="dcAbbreviation" SortExpression="dcAbbreviation"></ASP:BOUNDCOLUMN><ASP:BOUNDCOLUMN HeaderText="Message Number" DataField="messageNumber" SortExpression="messageNumber"></ASP:BOUNDCOLUMN><ASP:BOUNDCOLUMN HeaderText="Message" DataField="messageText" SortExpression="messageText"></ASP:BOUNDCOLUMN

    And, here's the code I am trying to bind with

    ParentGrid.DataSource = retData.messageDataCollection
    ParentGrid.DataBind()

    Can anyone tell me what I am doing wrong

    Thanks.
     
    Joe Rattz, Apr 22, 2004
    #1
    1. Advertising

  2. your datasource does not contain dsAbbreviation. the internal binding is
    looking for this column and cannot find it. change it to the correct column

    --
    Regards,
    Alvin Bruney
    [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    Got tidbits? Get it here... http://tinyurl.com/27cok
    "Joe Rattz" <> wrote in message
    news:...
    >I can't seem to bind an array of custom classes to my DataGrid. I get the
    >following error:
    >
    > "A field or property with the name 'dcAbbreviation' was not found on the
    > selected datasource:"
    >
    > This seems like it should be simple as falling off a log, but I can't find
    > it. I also can't seem to find any useful code or article on the web
    > either. Here are my specifics.
    >
    > Here's my class I have an array of:
    >
    >
    > [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:types:WEJOEI:genpt.com")]
    > public class messageData_t {
    >
    > /// <remarks/>
    > [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    > public string dcAbbreviation;
    >
    > /// <remarks/>
    > [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    > public string messageNumber;
    >
    > /// <remarks/>
    > [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    > public string messageText;
    > }
    >
    > You may recognize it as a generated web service proxy class...so don't ask
    > me to change it please.
    >
    > Here's my DataGrid Columns section:
    >
    > <ASP:BOUNDCOLUMN HeaderText="DC" DataField="dcAbbreviation"
    > SortExpression="dcAbbreviation"></ASP:BOUNDCOLUMN><ASP:BOUNDCOLUMN
    > HeaderText="Message Number" DataField="messageNumber"
    > SortExpression="messageNumber"></ASP:BOUNDCOLUMN><ASP:BOUNDCOLUMN
    > HeaderText="Message" DataField="messageText"
    > SortExpression="messageText"></ASP:BOUNDCOLUMN>
    >
    > And, here's the code I am trying to bind with:
    >
    > ParentGrid.DataSource = retData.messageDataCollection;
    > ParentGrid.DataBind();
    >
    > Can anyone tell me what I am doing wrong?
    >
    > Thanks.
     
    Alvin Bruney [MVP], Apr 22, 2004
    #2
    1. Advertising

  3. Hi bradaker,

    Based on my understanding, you want to implement a datasource for datagrid,
    and also enable the datagrid to bind to certain column.

    To act as a datasource of DataGrid control, you class should implement
    IEnumerable Interface, which normal array all implement this.(Your array of
    class should have meet this)

    But this only enable you to bind to the items in the IEnumerable interface.
    If you also want to enable internal Column databinding to certain field,
    your class should implement IListSource interface. You may view IListSource
    in MSDN:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
    frlrfSystemComponentModelIListSourceClassTopic.asp

    You typically use IListSource interface to return a list that can be bound
    to a data source, from an object that does not implement IList interface
    itself.(IList also implements IEnumerable interface)

    When binding a IListSource to datagrid, datagrid will search into the
    datasource internally, and bind each column. For more information, please
    refer to the article below:
    http://www.codeproject.com/aspnet/webcontrolsdatabinding.asp

    ====================================
    Please apply my suggestion above and let me know if it helps resolve your
    problem.

    Thank you for your patience and cooperation. If you have any questions or
    concerns, please feel free to post it in the group. I am standing by to be
    of assistance.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
     
    Jeffrey Tan[MSFT], Apr 23, 2004
    #3
  4. Joe Rattz

    Rick Spiewak Guest

    Instead of just declaring public variables, make them properties. Also, you
    can create a custom collection class instead of an array if you like, just
    inherit it from system.collections.collectionbase

    "Joe Rattz" <> wrote in message
    news:...
    > I can't seem to bind an array of custom classes to my DataGrid. I get the

    following error:
    >
    > "A field or property with the name 'dcAbbreviation' was not found on the

    selected datasource:"
    >
    > This seems like it should be simple as falling off a log, but I can't find

    it. I also can't seem to find any useful code or article on the web either.
    Here are my specifics.
    >
    > Here's my class I have an array of:
    >
    >

    [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:types:WEJOEI:genpt
    ..com")]
    > public class messageData_t {
    >
    > /// <remarks/>
    > [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    > public string dcAbbreviation;
    >
    > /// <remarks/>
    > [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    > public string messageNumber;
    >
    > /// <remarks/>
    > [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    > public string messageText;
    > }
    >
    > You may recognize it as a generated web service proxy class...so don't ask

    me to change it please.
    >
    > Here's my DataGrid Columns section:
    >
    > <ASP:BOUNDCOLUMN HeaderText="DC" DataField="dcAbbreviation"

    SortExpression="dcAbbreviation"></ASP:BOUNDCOLUMN><ASP:BOUNDCOLUMN
    HeaderText="Message Number" DataField="messageNumber"
    SortExpression="messageNumber"></ASP:BOUNDCOLUMN><ASP:BOUNDCOLUMN
    HeaderText="Message" DataField="messageText"
    SortExpression="messageText"></ASP:BOUNDCOLUMN>
    >
    > And, here's the code I am trying to bind with:
    >
    > ParentGrid.DataSource = retData.messageDataCollection;
    > ParentGrid.DataBind();
    >
    > Can anyone tell me what I am doing wrong?
    >
    > Thanks.
     
    Rick Spiewak, Apr 25, 2004
    #4
  5. Joe Rattz

    Joe Rattz Guest

    Thanks for the suggestion, but I can't/won't do that.. As I said, the class
    is GENERATED code...its a web service proxy class that has been generated by
    WSDL.exe.



    "Rick Spiewak" <> wrote in message
    news:%...
    > Instead of just declaring public variables, make them properties. Also,

    you
    > can create a custom collection class instead of an array if you like, just
    > inherit it from system.collections.collectionbase
    >
    > "Joe Rattz" <> wrote in message
    > news:...
    > > I can't seem to bind an array of custom classes to my DataGrid. I get

    the
    > following error:
    > >
    > > "A field or property with the name 'dcAbbreviation' was not found on the

    > selected datasource:"
    > >
    > > This seems like it should be simple as falling off a log, but I can't

    find
    > it. I also can't seem to find any useful code or article on the web

    either.
    > Here are my specifics.
    > >
    > > Here's my class I have an array of:
    > >
    > >

    >

    [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:types:WEJOEI:genpt
    > .com")]
    > > public class messageData_t {
    > >
    > > /// <remarks/>
    > > [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    > > public string dcAbbreviation;
    > >
    > > /// <remarks/>
    > > [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    > > public string messageNumber;
    > >
    > > /// <remarks/>
    > > [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    > > public string messageText;
    > > }
    > >
    > > You may recognize it as a generated web service proxy class...so don't

    ask
    > me to change it please.
    > >
    > > Here's my DataGrid Columns section:
    > >
    > > <ASP:BOUNDCOLUMN HeaderText="DC" DataField="dcAbbreviation"

    > SortExpression="dcAbbreviation"></ASP:BOUNDCOLUMN><ASP:BOUNDCOLUMN
    > HeaderText="Message Number" DataField="messageNumber"
    > SortExpression="messageNumber"></ASP:BOUNDCOLUMN><ASP:BOUNDCOLUMN
    > HeaderText="Message" DataField="messageText"
    > SortExpression="messageText"></ASP:BOUNDCOLUMN>
    > >
    > > And, here's the code I am trying to bind with:
    > >
    > > ParentGrid.DataSource = retData.messageDataCollection;
    > > ParentGrid.DataBind();
    > >
    > > Can anyone tell me what I am doing wrong?
    > >
    > > Thanks.

    >
    >
     
    Joe Rattz, Apr 26, 2004
    #5
  6. Joe Rattz

    Joe Rattz Guest

    I have found something that makes this work for me. I am positive that it
    is a fairly inefficient way to do this, but it does work. I suspect someone
    more familiar with this part of the .NET API's would probably know a more
    direct way to do this. Here is the code I came up with:

    StringWriter writer = new StringWriter();
    XmlSerializer serializer = new
    XmlSerializer(typeof(messageData_t[]));
    serializer.Serialize(writer, retData.messageDataCollection);
    DataSet ds = new DataSet();
    StringReader reader = new StringReader(writer.ToString());
    ds.ReadXml(reader);
    ParentGrid.DataSource =
    ds.Tables["messageData_t"].DefaultView;
    ParentGrid.DataBind();

    This serializes the array to XML and then reads the XML into a DataSet. I
    bet there is a more efficient way to do this though.

    Thanks.


    "Joe Rattz" <> wrote in message
    news:...
    > I can't seem to bind an array of custom classes to my DataGrid. I get the

    following error:
    >
    > "A field or property with the name 'dcAbbreviation' was not found on the

    selected datasource:"
    >
    > This seems like it should be simple as falling off a log, but I can't find

    it. I also can't seem to find any useful code or article on the web either.
    Here are my specifics.
    >
    > Here's my class I have an array of:
    >
    >

    [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:types:WEJOEI:genpt
    ..com")]
    > public class messageData_t {
    >
    > /// <remarks/>
    > [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    > public string dcAbbreviation;
    >
    > /// <remarks/>
    > [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    > public string messageNumber;
    >
    > /// <remarks/>
    > [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    > public string messageText;
    > }
    >
    > You may recognize it as a generated web service proxy class...so don't ask

    me to change it please.
    >
    > Here's my DataGrid Columns section:
    >
    > <ASP:BOUNDCOLUMN HeaderText="DC" DataField="dcAbbreviation"

    SortExpression="dcAbbreviation"></ASP:BOUNDCOLUMN><ASP:BOUNDCOLUMN
    HeaderText="Message Number" DataField="messageNumber"
    SortExpression="messageNumber"></ASP:BOUNDCOLUMN><ASP:BOUNDCOLUMN
    HeaderText="Message" DataField="messageText"
    SortExpression="messageText"></ASP:BOUNDCOLUMN>
    >
    > And, here's the code I am trying to bind with:
    >
    > ParentGrid.DataSource = retData.messageDataCollection;
    > ParentGrid.DataBind();
    >
    > Can anyone tell me what I am doing wrong?
    >
    > Thanks.
     
    Joe Rattz, Apr 26, 2004
    #6
  7. Hi Joe,

    Please ignore my orignal reply. I think I misunderstand your point in that
    reply.

    Yes, just as Rick said, you the databinding can not set data field for
    property. It can not bind datafield through public field.

    For your scenario of web service auto generated class, I think you have to
    write another custom class which wrap your auto generated class. For your
    custom class, you should write a property which just wrap the public field
    and return it as public.

    This should meet your need.

    Thanks

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
     
    Jeffrey Tan[MSFT], Apr 28, 2004
    #7
  8. Hi Joe,

    Thanks very much for your feedback.

    Yes, I just viewed your solution. I think it should be suitable for you and
    it should be nice :)

    Normally, not all Xml can be read into dataset properly, too "deep" xml may
    not compatible with DataSet. But for your class, I think it should have no
    this problem.

    Anyway, I think you solution is good in this scenario.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
     
    Jeffrey Tan[MSFT], Apr 29, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SW1hciBTcGFhbmphYXJz?=

    Databinding to custom properties of a custom class in ASP.NET 2

    =?Utf-8?B?SW1hciBTcGFhbmphYXJz?=, Apr 20, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    607
    =?Utf-8?B?SW1hciBTcGFhbmphYXJz?=
    Apr 20, 2005
  2. =?Utf-8?B?dWxyaWNoIHNjaHVtYWNoZXI=?=

    ASP.NET 2.0 DataBinding with object oriented business classes

    =?Utf-8?B?dWxyaWNoIHNjaHVtYWNoZXI=?=, Jun 30, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,484
    =?Utf-8?B?dWxyaWNoIHNjaHVtYWNoZXI=?=
    Jul 3, 2005
  3. jobs
    Replies:
    0
    Views:
    1,764
  4. Earl Teigrob
    Replies:
    1
    Views:
    444
    Shravan
    Mar 1, 2004
  5. Jed
    Replies:
    1
    Views:
    116
    Bob Barrows [MVP]
    Nov 10, 2004
Loading...

Share This Page