Creating A Gridview Programmatically

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

S. Justin Gengo [MCP]

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
 
B

Brian McClellan

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.)
 
S

S. Justin Gengo [MCP]

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.)
 
S

S. Justin Gengo [MCP]

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
 
Joined
Feb 12, 2008
Messages
2
Reaction score
0
Hi Brian,

If you want to create a drop down list instead of a textbox in the gridview you can easily do this. Click on the smart tag for the gridview and go to click on "Edit Columns". Find the column that you want to change from a textbox and click on "Convert this field into a TemplateField".

Close the box and return to the smart tag options and click on "Edit Templates" Any columns that you have turned into templateFields will show up as on option in the new view (you may have to click on the smart tag again). Each templated column will have a view for the Item {read only/default view}, Edit {what you see when the grid view is in edit mode...you can make things read only, or invisible here if you don't want them to be seen in edit mode}, also templates for Insert {insert view of the gridview insert mode}, the Footer and Header also.

So, if you want to see a dropdownlist instead of a text box in the Item template for example, you can simply delete the text box. Replace it with a dropdownlist control, rebind the new control to the correct field (the control will have its own smart tag that says, "Edit Databindings"). Name the new control if you want to access it programmatically.

For this example, will name it DropDownList1 (original, yes, I know :). So, in the code behind if you want to be able to access the information in that dropdownlist you could do the following:

Dim myDDL as DropDownList = CType(Me.GridView1.FindControl("DropDownList1"), DropDownList)
If Not myDDL is Nothing Then
myDDL.text = "Something I want it to say"
End If

As a note: you can add anything you want in these template fields. You can add a detailsView, FormView, whatever. You would access them programatically the same way. (me.gridview1.findcontrol("controlname")) then, you can access them. HINT: If you need to access a template field in a formview, for example, that is nested in a gridview template field you first need to "FindControl" the formview...then "FindControl" again the formview control.

Hope That Helps!

Briana Tarrance
www.virtual-essentials.com
www.howtodotnetnuke.com
 
Joined
Feb 12, 2008
Messages
2
Reaction score
0
If you want to create a drop down list instead of a textbox in the gridview you can easily do this. Click on the smart tag for the gridview and go to click on "Edit Columns". Find the column that you want to change from a textbox and click on "Convert this field into a TemplateField".

Close the box and return to the smart tag options and click on "Edit Templates" Any columns that you have turned into templateFields will show up as on option in the new view (you may have to click on the smart tag again). Each templated column will have a view for the Item {read only/default view}, Edit {what you see when the grid view is in edit mode...you can make things read only, or invisible here if you don't want them to be seen in edit mode}, also templates for Insert {insert view of the gridview insert mode}, the Footer and Header also.

So, if you want to see a dropdownlist instead of a text box in the Item template for example, you can simply delete the text box. Replace it with a dropdownlist control, rebind the new control to the correct field (the control will have its own smart tag that says, "Edit Databindings"). Name the new control if you want to access it programmatically.

For this example, will name it DropDownList1 (original, yes, I know . So, in the code behind if you want to be able to access the information in that dropdownlist you could do the following:

Dim myDDL as DropDownList = CType(Me.GridView1.FindControl("DropDownList1"), DropDownList)
If Not myDDL is Nothing Then
myDDL.text = "Something I want it to say"
End If

As a note: you can add anything you want in these template fields. You can add a detailsView, FormView, whatever. You would access them programatically the same way. (me.gridview1.findcontrol("controlname")) then, you can access them. HINT: If you need to access a template field in a formview, for example, that is nested in a gridview template field you first need to "FindControl" the formview...then "FindControl" again the formview control.

Hope That Helps!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,976
Members
47,533
Latest member
medikillz39

Latest Threads

Top