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

Creating A Gridview Programmatically

P: n/a
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
Share this Question
Share on Google+
5 Replies


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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.