473,383 Members | 1,876 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,383 software developers and data experts.

Creating A Gridview Programmatically

Just wondering if anyone has a simple example of creating a gridview
completely programmatically, i'm not doing anything terribly sophisticated.
When creating the gridview declaratively evertying works fine, however
programmatically, while the grid will display data that exsists in the
database, any operation on the data ( editing/updating/deleting ) seems to
cause a rowdeleting/updating etc error. Or is this simply not meant to be
done?
Jan 31 '06 #1
5 15255
Brian,

You can certainly create a gridview programmatically. The easiest example
would be to place an empty gridview on a page and then bind data to it:

(Assuming an already created datatable and GridView columns set to
autogenerate it would only be two lines of code)

GridView1.DataSource = MyDataTable
GridView1.DataBind

Of course the GridView itself may even be created programmatically and then
added to the page or more commonly a placeholder.

Dim GridView1 As New GridView
GridView1.DataSource = MyDataTable
GridView1.DataBind

MyPlaceholder.Controls.Add(GridView1)

You may even specify columns instead of using autogenerate, etc.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Brian McClellan" <BC*****@home.com> wrote in message
news:Xn***************************@216.196.97.131. ..
Just wondering if anyone has a simple example of creating a gridview
completely programmatically, i'm not doing anything terribly
sophisticated.
When creating the gridview declaratively evertying works fine, however
programmatically, while the grid will display data that exsists in the
database, any operation on the data ( editing/updating/deleting ) seems to
cause a rowdeleting/updating etc error. Or is this simply not meant to be
done?

Jan 31 '06 #2
"S. Justin Gengo [MCP]" <justin@[no_spam_please]aboutfortunate.com>
wrote in news:OK**************@TK2MSFTNGP14.phx.gbl:
Brian,

You can certainly create a gridview programmatically. The easiest
example would be to place an empty gridview on a page and then bind
data to it:

(Assuming an already created datatable and GridView columns set to
autogenerate it would only be two lines of code)

GridView1.DataSource = MyDataTable
GridView1.DataBind

Of course the GridView itself may even be created programmatically and
then added to the page or more commonly a placeholder.

Dim GridView1 As New GridView
GridView1.DataSource = MyDataTable
GridView1.DataBind

MyPlaceholder.Controls.Add(GridView1)

You may even specify columns instead of using autogenerate, etc.

default.aspx
<form id="form1" runat="server">
<div>
<asp:GridView id="g" runat="server" ></asp:GridView>
<asp:SqlDataSource id="s" runat="server" ></asp:SqlDataSource>
</div>
</form>

default.aspx.cs listing
//In the Page_Load Function
s.ConnectionString = @"..."
s.SelectCommand = "SELECT * FROM [Classes]";

g.DataSource = s.Select(DataSourceSelectArguments.Empty);
g.AutoGenerateEditButton = true;
g.DataBind();

I've been messing around a bit and while the grid data displays properly,
Clicking on the edit link gives this error.

The GridView 'g' fired event RowEditing which wasn't handled.

As the actutal method which fireing the RowEditing event is dynamically
generated i'm not sure how to go about handling it.

i've tried creating handlers for all of the events attached with editing
and deletion (same errors for those). Any ideas? ( I know its not
completely programmatic but as i'm learning these things i prefer to figure
out what exectly the asp.net declarative code is interpreted to, and there
don't seem to be many guides on doing thigns programmatically available
either in reference books or online.)
Feb 1 '06 #3
Brian,

I found this article which shows everything you need:

http://msdn2.microsoft.com/en-us/lib...nthandler.aspx

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Brian McClellan" <BC*****@home.com> wrote in message
news:Xn***************************@216.196.97.131. ..
"S. Justin Gengo [MCP]" <justin@[no_spam_please]aboutfortunate.com>
wrote in news:OK**************@TK2MSFTNGP14.phx.gbl:
Brian,

You can certainly create a gridview programmatically. The easiest
example would be to place an empty gridview on a page and then bind
data to it:

(Assuming an already created datatable and GridView columns set to
autogenerate it would only be two lines of code)

GridView1.DataSource = MyDataTable
GridView1.DataBind

Of course the GridView itself may even be created programmatically and
then added to the page or more commonly a placeholder.

Dim GridView1 As New GridView
GridView1.DataSource = MyDataTable
GridView1.DataBind

MyPlaceholder.Controls.Add(GridView1)

You may even specify columns instead of using autogenerate, etc.

default.aspx
<form id="form1" runat="server">
<div>
<asp:GridView id="g" runat="server" ></asp:GridView>
<asp:SqlDataSource id="s" runat="server" ></asp:SqlDataSource>
</div>
</form>

default.aspx.cs listing
//In the Page_Load Function
s.ConnectionString = @"..."
s.SelectCommand = "SELECT * FROM [Classes]";

g.DataSource = s.Select(DataSourceSelectArguments.Empty);
g.AutoGenerateEditButton = true;
g.DataBind();

I've been messing around a bit and while the grid data displays properly,
Clicking on the edit link gives this error.

The GridView 'g' fired event RowEditing which wasn't handled.

As the actutal method which fireing the RowEditing event is dynamically
generated i'm not sure how to go about handling it.

i've tried creating handlers for all of the events attached with editing
and deletion (same errors for those). Any ideas? ( I know its not
completely programmatic but as i'm learning these things i prefer to
figure
out what exectly the asp.net declarative code is interpreted to, and there
don't seem to be many guides on doing thigns programmatically available
either in reference books or online.)

Feb 1 '06 #4
"S. Justin Gengo [MCP]" <justin@[no_spam_please]aboutfortunate.com>
wrote in news:#h*************@tk2msftngp13.phx.gbl:
Brian,

I found this article which shows everything you need:

http://msdn2.microsoft.com/en-us/lib...bcontrols.grid
viewroweventhandler.aspx


Where are my manners, thanks very much for you replies they're apprciated
and that article help alot. There is one other small question i have, is
there a way to override the default text box edit, with say a drop down box
instead? Again thanks =)
Feb 2 '06 #5
Brian,

No thanks is necessary, but thank-you for the thanks!

Yes, of course, (you can do anything on a computer can't you) :-). But I'm
afraid you're entering territory I haven't played with yet. I'll look around
for an article on this but I've already taken a brief look and haven't come
up with anything useful yet.

Before moving ahead may I ask why you need to create the entire grid
programmatically? There are probably easier ways to create and change the
grid. Perhaps we should investigate a different path.
--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Brian McClellan" <BC*****@home.com> wrote in message
news:Xn****************************@216.196.97.131 ...
"S. Justin Gengo [MCP]" <justin@[no_spam_please]aboutfortunate.com>
wrote in news:#h*************@tk2msftngp13.phx.gbl:
Brian,

I found this article which shows everything you need:

http://msdn2.microsoft.com/en-us/lib...bcontrols.grid
viewroweventhandler.aspx


Where are my manners, thanks very much for you replies they're apprciated
and that article help alot. There is one other small question i have, is
there a way to override the default text box edit, with say a drop down
box
instead? Again thanks =)

Feb 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: theKirk | last post by:
using Visual Studio 2005 C# ASP.NET I know there has to be a simple way to do this....I want to use C# in a code behind for aspx. Populate a GridView from an xml file Add Fields to the...
1
by: | last post by:
I'm wondering if it's possible, outside of the declaration of an ASP.NET 2.0 GridView, to programmatically style individual columns. For example, I might want to make the text in a particular...
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...
3
by: Gabe | last post by:
Hi there, The other day I tried to programmatically create an asp.net linkbutton during my Page_Load() event and attach a click event handler to the linkbutton, then add that linkbutton control...
2
by: Philip | last post by:
I can use a GridView and ObjectDataSource to bind my data using "Eval" expressions ... provided the GridView and related Column TemplateFields are defined as inline HTML. I prefer to...
2
by: antonyliu2002 | last post by:
I've been googling for some time, and could not find the solution to this problem. I am testing the paging feature of gridview. I have a very simple web form on which the user can select a few...
4
by: Tomasz | last post by:
Hello Developers, Here is interesting problem I just came across: how do I wire a GridView control programmatically? Here is my sample code using Object Data Source: protected void...
0
by: Don Miller | last post by:
Without a SQLDataSource declaratively linked to a GridView, I programmatically create a dataset and bind it to a GridView with EnableSortingAndPagingCallbacks=True and Paging=True. The GridView...
7
by: =?Utf-8?B?V2FubmFiZQ==?= | last post by:
Is there a way to use a gridview in a timecard application, and if so, how? I was looking at using a gridview to display a person's hours worked in a week. To do this, many different data records...
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
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.