Solved

Dynamic Gridview - binding more than one column from an objectdatasource

Posted on 2006-11-07
1
321 Views
Last Modified: 2008-01-09
I have a gridview (Gridview1) defined as follows:-

<asp: GridView ID="Gridview1" runat = "server" AutoGenerateColumns="false" DataSourceID="ObjectDataSource1">
<Columns>
</Columns>
</asp:Gridview>

and columns dynamically added using

private void BuildGridView()
{
BoundField column1 = new BoundField();
column1.DataField = "ID";
column1.HeaderText = "ID"
GridView1.Columns.Add(column1);

BoundField column2 = new BoundField();
column2.DataField = "FirstName";
column2.HeaderText = "FirstName"
GridView1.Columns.Add(column2);

}
 
How do I "tidy up" this code (using eg "foreach") so I do not have to specify each individual column in the objectdatasouce.

 (ie how do I automatically bind all the columns in the objectdatasource?)

Thanks !
0
Comment
Question by:doolinn
1 Comment
 
LVL 11

Accepted Solution

by:
ethoths earned 500 total points
ID: 17888942
Set autogeneratecolumns to true...

GridView1.AutoGenerateColumns = True

and drop all the other code.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now