By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,206 Members | 1,035 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,206 IT Pros & Developers. It's quick & easy.

How to bind data to DataGrid or Grid View

P: n/a
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
Oct 16 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <vi*********@discussions.microsoft.comwrote in message
news:0B**********************************@microsof t.com...
>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

Oct 16 '06 #2

P: n/a
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" wrote:
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" <vi*********@discussions.microsoft.comwrote in message
news:0B**********************************@microsof t.com...
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


Oct 16 '06 #3

P: n/a
Provided you set grid.DataSource=ar, your example should work.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
"vineetbatta" <vi*********@discussions.microsoft.comwrote in message
news:54**********************************@microsof t.com...
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" wrote:
>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" <vi*********@discussions.microsoft.comwrote in message
news:0B**********************************@microso ft.com...
>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



Oct 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.