473,378 Members | 1,207 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Adding data to a Grid View programmatically

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
2 6702
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: pmud | last post by:
Hi, I need to display columns in a data grid based on 7 different queries. Now I have 32 questions: 1. Is it possble to have 1 single data adapter with 7 queries & 1 data set or do I need to...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
3
by: Simon Harris | last post by:
Hi All, I have a data grid which displays country names. I now wish to display the country flag images above the names. Can someone please advise how I display an image in a datagrid? I have a...
10
by: John Wilson | last post by:
My app produces some long datatables to display in a grid. So I put them in a div so users can scroll. But the grid headers scroll out of view. I would like to stop them doing this. Can I fix them...
7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
2
by: bob | last post by:
After adding a new row (programmatically) to a table/view that is being displayed in the DataGridView control, the new row appears in the DataGridView grid. Now, how do I select that new row? If...
2
by: moondaddy | last post by:
I'm trying to do something real simple. Add a thumb to a grid at the end of a line using c#. the code executes but I dont see the thumb. Can anyone explain what and what I need to do to properly...
0
by: db007 | last post by:
I have a problem at the moment with a web project. I have two Panels within an UpdatePanel on an aspx page (using Masterpages). I'm using ASP.Net 2.0 with an AJAX enabled website. The two...
6
by: insirawali | last post by:
Hi all, I have this problem, i need to know is there a way i cn use the data adapter's update method in this scenario. i have 3 tables as below create table table1{ id1 int identity(1,1)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.