Creating A Gridview Programmatically

  • Thread starter Thread starter Brian McClellan
  • Start date Start date
B

Brian McClellan

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?
 
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,

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.)
 
Brian,

I found this article which shows everything you need:

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewroweventhandler.aspx



--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
Brian McClellan said:
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.)
 
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
 
Back
Top