Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Databinding array of custom classes to DataGrid

Reply
Thread Tools

Databinding array of custom classes to DataGrid

 
 
Joe Rattz
Guest
Posts: n/a
 
      04-22-2004
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(Namespac e="urn:types:WEJOEI:genpt.com")
public class messageData_t

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

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

/// <remarks/
[System.Xml.Serialization.XmlElementAttribute(IsNul lable=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.
 
Reply With Quote
 
 
 
 
Alvin Bruney [MVP]
Guest
Posts: n/a
 
      04-22-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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(Namespac e="urn:types:WEJOEI:genpt.com")]
> public class messageData_t {
>
> /// <remarks/>
> [System.Xml.Serialization.XmlElementAttribute(IsNul lable=true)]
> public string dcAbbreviation;
>
> /// <remarks/>
> [System.Xml.Serialization.XmlElementAttribute(IsNul lable=true)]
> public string messageNumber;
>
> /// <remarks/>
> [System.Xml.Serialization.XmlElementAttribute(IsNul lable=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.



 
Reply With Quote
 
 
 
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      04-23-2004
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/de...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/we...atabinding.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.

 
Reply With Quote
 
Rick Spiewak
Guest
Posts: n/a
 
      04-25-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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(Namespac e="urn:types:WEJOEI:genpt
..com")]
> public class messageData_t {
>
> /// <remarks/>
> [System.Xml.Serialization.XmlElementAttribute(IsNul lable=true)]
> public string dcAbbreviation;
>
> /// <remarks/>
> [System.Xml.Serialization.XmlElementAttribute(IsNul lable=true)]
> public string messageNumber;
>
> /// <remarks/>
> [System.Xml.Serialization.XmlElementAttribute(IsNul lable=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.



 
Reply With Quote
 
Joe Rattz
Guest
Posts: n/a
 
      04-26-2004
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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(Namespac e="urn:types:WEJOEI:genpt
> .com")]
> > public class messageData_t {
> >
> > /// <remarks/>
> > [System.Xml.Serialization.XmlElementAttribute(IsNul lable=true)]
> > public string dcAbbreviation;
> >
> > /// <remarks/>
> > [System.Xml.Serialization.XmlElementAttribute(IsNul lable=true)]
> > public string messageNumber;
> >
> > /// <remarks/>
> > [System.Xml.Serialization.XmlElementAttribute(IsNul lable=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.

>
>



 
Reply With Quote
 
Joe Rattz
Guest
Posts: n/a
 
      04-26-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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(Namespac e="urn:types:WEJOEI:genpt
..com")]
> public class messageData_t {
>
> /// <remarks/>
> [System.Xml.Serialization.XmlElementAttribute(IsNul lable=true)]
> public string dcAbbreviation;
>
> /// <remarks/>
> [System.Xml.Serialization.XmlElementAttribute(IsNul lable=true)]
> public string messageNumber;
>
> /// <remarks/>
> [System.Xml.Serialization.XmlElementAttribute(IsNul lable=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.



 
Reply With Quote
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      04-28-2004
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.

 
Reply With Quote
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      04-29-2004
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Databinding expressions are only supported on objects that have a DataBinding event jobs ASP .Net 0 09-25-2007 11:54 PM
Gridview encoding, or how to run commands before gridview's default databinding, or, how do I disable default databinding at all? Sergei Shelukhin ASP .Net 1 11-12-2006 01:43 PM
Databinding to custom properties of a custom class in ASP.NET 2 =?Utf-8?B?SW1hciBTcGFhbmphYXJz?= ASP .Net 0 04-20-2005 07:34 PM
DataBinding to Array of Custom Objects Jed ASP General 1 11-10-2004 06:04 PM
DataGrid Custom Column Error when DataBinding "does not contain a definition for 'DataBinding'" Earl Teigrob ASP .Net Datagrid Control 1 03-01-2004 04:52 AM



Advertisments