How to bind data to DataGrid or Grid View

G

Guest

I have Custom Data class which stores data about single customer and then i
store that customer objects in arraylist as shown below.

Customer custdata = null; // Custom Data class for 1 customer data.

ArrayList ar = new ArrayList(); // To store more than one customer
object.
for (int x = 0; x < 30; x++)
{
custdata = new Customer();
custdata.CustomerID = 1234 + x;
custdata.CustomerName = "Name" + x.ToString();
custdata.CustomerLocation = "UK " + x.ToString();
custdata.CustomerType = x.ToString();

ar.Add(custdata);

}

grid.; /// FEW Steps are missing and not sure to make it compatable to Grid.
grid.DataBind();

Now i want to bind data to gridView control, just like as we do if it was
Datatable.
Is there any simple conversion possible?

I do not want to use DataSet or DataTable but collection of Customer Objects
in Arraylist.

Thanks in advance.


-Vineet
 
E

Eliyahu Goldin

Vineet,

The easiest way is to use Eval method. In the code assign grid.DataSource=ar
and in the aspx page use databinding expressions like

<td><%# Eval("Name") %></td>
 
G

Guest

Hi Eliyahu,

Is there any way we can use properties like Data member/ Data Key on grid
object just like for DataTable or DataSet?


I want to retain aspx code like

<Columns >
<asp:BoundField DataField = "CustomerID" HeaderText = "ID" />
<asp:BoundField DataField = "CustomerName" HeaderText = "Name" />
<asp:BoundField DataField = "CustomerLocation" HeaderText =
"Location" />
</Columns>

Thanks


Eliyahu Goldin said:
Vineet,

The easiest way is to use Eval method. In the code assign grid.DataSource=ar
and in the aspx page use databinding expressions like

<td><%# Eval("Name") %></td>

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


vineetbatta said:
I have Custom Data class which stores data about single customer and then i
store that customer objects in arraylist as shown below.

Customer custdata = null; // Custom Data class for 1 customer data.

ArrayList ar = new ArrayList(); // To store more than one customer
object.
for (int x = 0; x < 30; x++)
{
custdata = new Customer();
custdata.CustomerID = 1234 + x;
custdata.CustomerName = "Name" + x.ToString();
custdata.CustomerLocation = "UK " + x.ToString();
custdata.CustomerType = x.ToString();

ar.Add(custdata);

}

grid.; /// FEW Steps are missing and not sure to make it compatable to
Grid.
grid.DataBind();

Now i want to bind data to gridView control, just like as we do if it was
Datatable.
Is there any simple conversion possible?

I do not want to use DataSet or DataTable but collection of Customer
Objects
in Arraylist.

Thanks in advance.


-Vineet
 
E

Eliyahu Goldin

Provided you set grid.DataSource=ar, your example should work.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


vineetbatta said:
Hi Eliyahu,

Is there any way we can use properties like Data member/ Data Key on grid
object just like for DataTable or DataSet?


I want to retain aspx code like

<Columns >
<asp:BoundField DataField = "CustomerID" HeaderText = "ID" />
<asp:BoundField DataField = "CustomerName" HeaderText = "Name" />
<asp:BoundField DataField = "CustomerLocation" HeaderText =
"Location" />
</Columns>

Thanks


Eliyahu Goldin said:
Vineet,

The easiest way is to use Eval method. In the code assign
grid.DataSource=ar
and in the aspx page use databinding expressions like

<td><%# Eval("Name") %></td>

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


vineetbatta said:
I have Custom Data class which stores data about single customer and
then i
store that customer objects in arraylist as shown below.

Customer custdata = null; // Custom Data class for 1 customer data.

ArrayList ar = new ArrayList(); // To store more than one
customer
object.
for (int x = 0; x < 30; x++)
{
custdata = new Customer();
custdata.CustomerID = 1234 + x;
custdata.CustomerName = "Name" + x.ToString();
custdata.CustomerLocation = "UK " + x.ToString();
custdata.CustomerType = x.ToString();

ar.Add(custdata);

}

grid.; /// FEW Steps are missing and not sure to make it compatable to
Grid.
grid.DataBind();

Now i want to bind data to gridView control, just like as we do if it
was
Datatable.
Is there any simple conversion possible?

I do not want to use DataSet or DataTable but collection of Customer
Objects
in Arraylist.

Thanks in advance.


-Vineet
 

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,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top