Adding data to a Grid View programmatically

W

Wayne Deleer

Hi All,

I'm currently working on a project, and I've come to a part where I
want to add data to a GridView. BUT, I want to do it programmatically,
with alot of control over everything. In my particular project I've
passed a DataSet to a page via a Session object. The Session object
passes without incident, and I can use the default settings to retrieve
the information without problem and plug all the information into the
GridView.

However, currently what I'm doing is configuring the .aspx page with
all the appropriate settings (eg. AutoGenerateFields=false, define my
columns, etc...) to list only the columns I want using the BoundField
tag. What I'd like to do is do it all programmatically in the aspx.cs
file.

So far, my little experiment looks like this:
// Note that grd_SearchResults is the GridView control on the .aspx
// page
DataControlField df1 = new BoundField();
df1.HeaderText = "test";
grd_SearchResults.Columns.Add(df1);


//grd_SearchResults.DataSource = SearchResult;
//grd_SearchResults.DataBind();

So far, I can create the column named "test" but the bugger has been
trying to put the data I want into it. Ideally I want to just select
what column from the DataSet to include and go from there.

If there was some kind of control/command where I could type
df1.DataField="index" or df1.DataField = SearchResult["index"], I think
I'd be all set, but there's no such control/command.

The main reason I want to this is that I can forsee in the future there
will be changes to this project. So the .aspx page is just meant to
represent how I want my data layed out, and the aspx.cs file is meant
to put the data into it. So... in the future, I (or someone else)
will be able to just mess with the code-behind only. Basically, for
ease of maintenance.

I'm sure there's a way of doing it. It could be I'll have to dig into
things a bit more and make things more complicated too. But, oh well,
guess that's just part of learning :)

Thanks for any input in advance,
Wayne D.
 
D

dunny

Hi Wayne

Try changing:

DataControlField df1 = new BoundField(); to
BoundField df1 = new BoundField();

You'll then have access to the BoundField's DataField property.

HTH
Mark
 
W

Wayne Deleer

Sweet, thanks dunny.

Hi Wayne

Try changing:

DataControlField df1 = new BoundField(); to
BoundField df1 = new BoundField();

You'll then have access to the BoundField's DataField property.

HTH
Mark

Wayne said:
Hi All,

I'm currently working on a project, and I've come to a part where I
want to add data to a GridView. BUT, I want to do it programmatically,
with alot of control over everything. In my particular project I've
passed a DataSet to a page via a Session object. The Session object
passes without incident, and I can use the default settings to retrieve
the information without problem and plug all the information into the
GridView.

However, currently what I'm doing is configuring the .aspx page with
all the appropriate settings (eg. AutoGenerateFields=false, define my
columns, etc...) to list only the columns I want using the BoundField
tag. What I'd like to do is do it all programmatically in the aspx.cs
file.

So far, my little experiment looks like this:
// Note that grd_SearchResults is the GridView control on the .aspx
// page
DataControlField df1 = new BoundField();
df1.HeaderText = "test";
grd_SearchResults.Columns.Add(df1);


//grd_SearchResults.DataSource = SearchResult;
//grd_SearchResults.DataBind();

So far, I can create the column named "test" but the bugger has been
trying to put the data I want into it. Ideally I want to just select
what column from the DataSet to include and go from there.

If there was some kind of control/command where I could type
df1.DataField="index" or df1.DataField = SearchResult["index"], I think
I'd be all set, but there's no such control/command.

The main reason I want to this is that I can forsee in the future there
will be changes to this project. So the .aspx page is just meant to
represent how I want my data layed out, and the aspx.cs file is meant
to put the data into it. So... in the future, I (or someone else)
will be able to just mess with the code-behind only. Basically, for
ease of maintenance.

I'm sure there's a way of doing it. It could be I'll have to dig into
things a bit more and make things more complicated too. But, oh well,
guess that's just part of learning :)

Thanks for any input in advance,
Wayne D.
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top