Databinding array of custom classes to DataGrid

J

Joe Rattz

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.
 
A

Alvin Bruney [MVP]

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

Jeffrey Tan[MSFT]

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.
 
R

Rick Spiewak

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 said:
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"
 
J

Joe Rattz

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 said:
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 said:
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"> said:
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.
 
J

Joe Rattz

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 said:
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"
 
J

Jeffrey Tan[MSFT]

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.
 
J

Jeffrey Tan[MSFT]

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top