using a DataGrid with something other than a DataTable

A

Andy Fish

Hi,

I have some data which is stored as an array (or possibly an arraylist) of
identically typed objects, each of which has certain public properties. I
want to display these in a datagrid control.

According to the documentation, the datasource of a datagrid can be anything
which implements IEnumerable (which includes array and arraylist). However,
every example I've seen for datagrid uses a DataTable to store the data.

is there any way to achieve this?

here's a sample code snippet which doesn't work but I hope it illustrates
what I'm trying to do:

class person {
string firstname;
string lastname;
}

person[] people = GetListOfPeople();

DataGrid.dataSource = people;
DataGrid.columns.add(new BoundColumn("firstname"))
DataGrid.columns.add(new BoundColumn("lastname"))
DataGrid.DataBind();

many thanks

Andy
 
M

mortb

Yes, I do this a lot in my applicaiton.
The only difference that I can see to your code is that I set the columns
inside the <asp:datagrid> tag in the design
I suggest you try your code it might work :)

cheers,
mortb
 
A

Andy Fish

aha - this is what I had missed.

I was using public fields (sorry - I missed the public off my code snippet).
when I put in some property accessors it worked.

Unfortunately it will be a bit of a PITA as the objects I'm trying to
display don't have accessors so I will have to wrap them in another object
:-(

In the meantime I have also discovered template columns which I think may
allow me to access the properties directly. So hopefully I have a couple of
workable options.

Thanks for your help

Andy


Bharat Biyani said:
Hi Andy,

To make the object bindable to the datagrid u will need to expose the
variables of the class as the properties of the Person object. Then u will be
able to bind the properties. Here is a code fragment which will help u make
things clearer.
Set the AutoGenerateColumns property of datagrid to false.

Person[] p =new Person[2];
p[0]=new Person("John","Wright");
p[1]=new Person("John","abraham");
DataGrid1.DataSource=p;
BoundColumn bc=new BoundColumn();
bc.DataField="FName";
bc.HeaderText="First Name";
DataGrid1.Columns.Add(bc);
bc=new BoundColumn();
bc.DataField="LName";
bc.HeaderText="Last Name";
DataGrid1.Columns.Add(bc);
DataGrid1.DataBind();

---
Bharat Biyani ([email protected])
http://www.orcim.com
Andy Fish said:
Hi,

I have some data which is stored as an array (or possibly an arraylist) of
identically typed objects, each of which has certain public properties. I
want to display these in a datagrid control.

According to the documentation, the datasource of a datagrid can be anything
which implements IEnumerable (which includes array and arraylist). However,
every example I've seen for datagrid uses a DataTable to store the data.

is there any way to achieve this?

here's a sample code snippet which doesn't work but I hope it illustrates
what I'm trying to do:

class person {
string firstname;
string lastname;
}

person[] people = GetListOfPeople();

DataGrid.dataSource = people;
DataGrid.columns.add(new BoundColumn("firstname"))
DataGrid.columns.add(new BoundColumn("lastname"))
DataGrid.DataBind();

many thanks

Andy
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top