Binding an array of objects to Data Grid

  • Thread starter Thread starter Abhishek Srivastava
  • Start date Start date
A

Abhishek Srivastava

Hello All,

Has anyone tried binding an Array of objects to a DataGrid, when the
array of objects is being returned by a Web Service?

I am putting an object called ModuleVO in my array. I have defined
properties for each member.

But when I add a webreference to my web service application, the
webreference proxy object for ModuleVO only has member variables but no
properties.

and the bind to the datagrid fails.

Has anyone tried binding an array of objects (returned by a webservice)
to a data grid successfully?

Please help me out. Thanks for your help in advance.

regards,
Abhishek.
 
There is an article about this topic:
http://tinyurl.com/2ttfd
Data binding and Web Services using Custom Collections
Summary:

The data binding capabilities in .NET are great, you can bind many controls
to almost any type of data, but in some scenarios data binding has its
limitations. For example data binding is not possible when using custom
collections, instead of DataSets, coming from a Web Service. This article
explains the problem and a possible, easy-to-use, solution: a class that
dynamically builds wrapper classes at run time, which exposes the field
member of the proxy classes as properties.
 
Hello Jan,

Thanks for your reply. Very nice article.

However if I have a "class that dynamically builds wrapper classes at
run time" What will happen to performance of my application.

Why doesn't the proxy code generator generate proxy objects with
properties? It won't harm the system anyway if someone starts to use
properties when he intended to use member-variable. However, the other
way round is harmfull as we have seen that data binding stops working.

Is this recognized as a bug in VS.Net?

regards,
Abhishek.
 
I am not sure if I misunderstood your question but as far as I know it is
very easy to bing and array of object to a datagrid.

let say your object ModuleVO got the following public properties:
ID
Age
Height

let say u want to display a datagrid made of ID and Age
then in ur aspx code u will have somethign like

<asp:DataGrid id="dataGrid1"
AutoGenerateColumns="False"
runat="server">
<Columns>
<asp:BoundColumn DataField="Id" HeaderText="Code" />
<asp:BoundColumn DataField="Age"
HeaderText="Age of the person" />
</Columns>
</asp:DataGrid>

in turn in the code behind code something like normal binding:

dataGrid1.DataSource=arrayOfModuleVO;
dataGrid1.DataBind( );

Note that I am pretty sure it works with arrayOfModuleVO being an ArrayList
of ModuleVO, but I never tried with something like arrayOfModuleVO being a
plain array (ModuleVO[])
Anyway u can generate an ArrayList easily from any array in case it does not
work with ModuleVO[]

if you use in the aspx something like
<asp:DataGrid id="dataGrid1" runat="server" />

it will generate one column in the datagrid for every public property you
have in your object.

Sorry for the email not very well presented neither well explained but i am
a little bit in a rush. I think you should be able to get what i mean tho.

Also please let me know tho if it works with ModuleVO[] instead of an
ArrayList of ModuleVO, I would appreciate to know that.

I hope this help

Francois
 
Hello Francois,

Read the article given by Jan Tielens. Data Binding of arrays will not
work if the array of objects is *returned by a web service*.

The problem seems to be that the proxy removes the properties from the
object. However, for databinding to work for array of objects,
properties are required.

This seems to be a Bug in the web service proxy generator.

regards,
Abhishek.
francois said:
I am not sure if I misunderstood your question but as far as I know it is
very easy to bing and array of object to a datagrid.

let say your object ModuleVO got the following public properties:
ID
Age
Height

let say u want to display a datagrid made of ID and Age
then in ur aspx code u will have somethign like

<asp:DataGrid id="dataGrid1"
AutoGenerateColumns="False"
runat="server">
<Columns>
<asp:BoundColumn DataField="Id" HeaderText="Code" />
<asp:BoundColumn DataField="Age"
HeaderText="Age of the person" />
</Columns>
</asp:DataGrid>

in turn in the code behind code something like normal binding:

dataGrid1.DataSource=arrayOfModuleVO;
dataGrid1.DataBind( );

Note that I am pretty sure it works with arrayOfModuleVO being an ArrayList
of ModuleVO, but I never tried with something like arrayOfModuleVO being a
plain array (ModuleVO[])
Anyway u can generate an ArrayList easily from any array in case it does not
work with ModuleVO[]

if you use in the aspx something like
<asp:DataGrid id="dataGrid1" runat="server" />

it will generate one column in the datagrid for every public property you
have in your object.

Sorry for the email not very well presented neither well explained but i am
a little bit in a rush. I think you should be able to get what i mean tho.

Also please let me know tho if it works with ModuleVO[] instead of an
ArrayList of ModuleVO, I would appreciate to know that.

I hope this help

Francois


Hello All,

Has anyone tried binding an Array of objects to a DataGrid, when the
array of objects is being returned by a Web Service?

I am putting an object called ModuleVO in my array. I have defined
properties for each member.

But when I add a webreference to my web service application, the
webreference proxy object for ModuleVO only has member variables but no
properties.

and the bind to the datagrid fails.

Has anyone tried binding an array of objects (returned by a webservice)
to a data grid successfully?

Please help me out. Thanks for your help in advance.

regards,
Abhishek.
 
I've used this wrapper quite some times, only the first time you use the
wrapper, there is a slight performance drop down.
Why doesn't the proxy code generator generate proxy objects with
properties?
Good question!

Maybe you could take a look at Christian's tool too:
http://weblogs.asp.net/cweyer/archive/2003/11/21/39070.aspx
New features in 0.2 (download link at the end of the article):
a.. Adding to the service side stub code generation, it now adds an .asmx
default implementation (automatic WSDL generation disabled, obviously).
b.. The client side developer gets access to the raw SOAP message - either
as a byte[] or simply as a string.
c.. Do you hate that wsdl.exe and the Visual Studio .NET "Add Web
Reference ..." dialog always and only can produce public fields for members
in data classes (based on the XSD)?
This tool can optionally generate private fields with public property
wrappers.
d.. Dynamic vs. static URL reference (you can configure the Web service
endpoint URL either in App.config or Web.config).
e.. Serveral bug fixes and internal improvements
 
Back
Top