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

Adding data to a Grid View programmatically

P: n/a
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.

Dec 21 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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 Deleer wrote:
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.
Dec 22 '06 #2

P: n/a
Sweet, thanks dunny.
dunny wrote:
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 Deleer wrote:
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.
Dec 22 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.