adding insert capabilities to the gridview
adding insert capabilities to the gridview
ivan porto carrero
placeholder.add("really cool stuff");
home |
contact |
syndication
|
login
49
posts |
6 stories
|
36 comments
|
90 trackbacks
news
article categories
c#
c# 2.0
archives
july, 2006 (3)
june, 2006 (8)
may, 2006 (12)
march, 2006 (2)
january, 2006 (2)
december, 2005 (8)
november, 2005 (3)
september, 2005 (4)
august, 2005 (7)
post categories
(rss)
.net 2.0 (rss)
.net 2.0 (rss)
atlas (rss)
c# (rss)
general (rss)
webstock 2006 (rss)
xslt (rss)
personal links
miel (coolz0r's blog)
my belgian company site
my old blogger blog
my old msn space
adding insert capabilities to the gridview
i didn't have time to wrap it in a proper control yet. in this post i'll just be putting the page implementation.
the gridview is cool and to add an insertrow to it can be done by the footer template. how to do it i explained a while ago : http://geekswithblogs.net/casualjim/articles/51360.aspx
if you want to get the controls from the footerrow you will need to address them with gridview.footerrow.findcontrol("controlname");.
for the empty datatemplate it's a little bit trickier but not that much. it still is a gridviewrow but it's wrapped in another control. if you use a button control in your empty datatemplate you also can't use the submit behaviour from then on everything should be familiar. :)
this is the part in the page :
<%@ page language="c#" theme="" autoeventwireup="true" codefile="test.aspx.cs" inherits="test" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head id="head1" runat="server"> <title>asp.net insert data in gridview </title> </head><body> <form id="form1" runat="server"> <asp:label id="label1" runat="server" text="label"></asp:label> <asp:gridview id="gridview1" showfooter="true" runat="server" onrowcommand="gridview1_rowcommand1" autogeneratecolumns="false"> <columns> <asp:templatefield> <itemtemplate> <asp:button text="edit" commandname="edit" causesvalidation="false" runat="server" id="btedit" /> <asp:button text="delete" commandname="delete" causesvalidation="false" runat="server" id="btdelete" /> </itemtemplate> <edititemtemplate> <asp:button text="update" commandname="update" causesvalidation="true" runat="server" id="btupdate" /> <asp:button text="cancel" commandname="cancel" causesvalidation="false" runat="server" id="btcancel" /> </edititemtemplate> <footertemplate> <asp:button text="insert" commandname="insert" causesvalidation="true" runat="server" id="btinsert" /> <asp:button text="cancel" commandname="cancel" causesvalidation="false" runat="server" id="btcancel" /> </footertemplate> </asp:templatefield> <asp:templatefield > <itemtemplate> <asp:label id="lblvalue" text='<%# eval("name") %>' runat="server"></asp:label> </itemtemplate> <edititemtemplate> <asp:textbox id="tbupdate" runat="server" text='<% bind("name") %>'></asp:textbox> </edititemtemplate> <footertemplate> <asp:textbox id="tbinsert" runat="server" text="" ></asp:textbox> </footertemplate> </asp:templatefield> </columns> <emptydatatemplate> <asp:textbox id="tbemptyinsert" runat="server"></asp:textbox><br /> <asp:button id="btsend" text="insert" runat="server" commandname="emptyinsert" usesubmitbehavior="false" /> </emptydatatemplate> </asp:gridview> </form> </body></html>
and the code behind :
14 public partial class test : system.web.ui.page
15 {
16 protected void page_load(object sender, eventargs e)
17 {
18 if (!ispostback)
19 {
20 //create dummy data
21 datatable dt = new datatable();
22 datacolumn dc = new datacolumn("name");
23 dt.columns.add(dc);
24 datarow dr = dt.newrow();
25 dr["name"] = "ivan";
26
27 //uncomment the following line to have data in the grid :)
28 //dt.rows.add(dr);
29
30 //bind the gridview
31 gridview1.datasource = dt;
32 gridview1.databind();
33 }
34 //recurses through the controls to show the naming of each individual control that is currently in the gridview
35 recursecontrols(gridview1.controls[0].controls);
36 label1.text += gridview1.controls[0].controls[0].gettype().name + "<br />";
37 }
38
39 void recursecontrols(controlcollection ctls)
40 {
41 foreach (control ctl in ctls)
42 {
43 if (!ctl.hascontrols())
44 label1.text += ctl.clientid + " " + ctl.gettype().name + "<br />";
45 else
46 recursecontrols(ctl.controls);
47 }
48 }
49
50 protected void gridview1_rowcommand1(object sender, gridviewcommandeventargs e)
51 {
52 if (e.commandname == "emptyinsert")
53 {
54 //handle insert here
55 textbox tbemptyinsert = gridview1.controls[0].controls[0].findcontrol("tbemptyinsert") as textbox;
56 label1.text = string.format("you would have inserted the name : <b>{0}</b> from the emptydatatemplate",tbemptyinsert.text);
57
58 }
59 if (e.commandname == "insert")
60 {
61 //handle insert here
62 textbox tbinsert = gridview1.footerrow.findcontrol("tbinsert") as textbox;
63 label1.text = string.format("you would have inserted the name : <b>{0}</b> from the footerrow", tbinsert.text);
64 }
65 }
66
67 }
posted on thursday, may 04, 2006 11:32 am
feedback
# re: adding insert capabilities to the gridview
6/13/2006 5:41 pm
syed sadiq
hi ivan porto carrero,
this article is really useful.
thank you
syed
# re: adding insert capabilities to the gridview
8/3/2006 1:07 pm
rony
this is an excellent article.. thanks. but i am having a problem. when i use the <emptydatatemplate> i can see the row but can not see the header fields.is there a way to show the header fields for empty datasets.
thanks
# re: adding insert capabilities to the gridview
12/15/2006 5:38 pm
souvik
thanx..helped me solve a long standing issue with an application i am developing!
# re: adding insert capabilities to the gridview
1/3/2007 12:42 am
niko
great!!! very powerful!
# re: adding insert capabilities to the gridview
1/20/2007 1:59 am
simon
this is an excellent article.. thanks. but i am having a problem. when i use the <emptydatatemplate> i can see the row but can not see the header fields.is there a way to show the header fields for empty datasets.
thanks
have the same problem?? anyone knows the answer??
# re: adding insert capabilities to the gridview
1/20/2007 6:40 am
ivan porto carrero
to show the headers of the rows you will need to add them yourself. the empty data template doesn't render a table because there is no data. no table means no headers.. you are responsible for the necessary information
# re: adding insert capabilities to the gridview
2/23/2007 7:07 am
charlie
this is much easier to follow than the first version. however, i'm unable to get the data inserted into the database. when i click on the insert nothing happens.
# re: adding insert capabilities to the gridview
2/23/2007 8:05 am
ivan porto carrero
you need to handle inserts in the row command event of the gridview. the code of my post here only demo's the page portion of the insert.
gridview1_rowcommand1
# re: adding insert capabilities to the gridview
5/2/2007 11:14 am
joel
awesome
# re: adding insert capabilities to the gridview
5/9/2007 3:49 pm
john
my data table is initally empty. i add values to the textboxes and hit insert data and it works fine. the input data is now displayed in the gridview and a row of blank textboxes is displayed in the footer. however, when i add values to this blank row and hit insert, it wipes out all data from the data table and treats it like an emptydata table again. the onrowcommand is not getting fired the second time around and the e.commandname is never "insert" and is always "insertempty". what could be the problem?
# re: adding insert capabilities to the gridview
5/9/2007 4:16 pm
ivan porto carrero
i'm guessing that you didn't set the command name of the button in the footerrow to "insert".
# re: adding insert capabilities to the gridview
5/10/2007 10:16 am
john
got it. i was binding the datasource on each postback, without saving the data table to the database, hence the inserted data was not getting saved and the data table was empty each time. this works great now. thanks, ivan!
# edititemtemplate in gridview
5/10/2007 3:47 pm
john
i am new to asp.net and hence struggling here. how do i handle the edit in the rowcommand? i need to display the edititemtemplate of that row. thanks in advance.
# re: adding insert capabilities to the gridview
5/10/2007 3:55 pm
ivan porto carrero
insert is not a standard command that is why you need to handle those in the rowcommand event.edit, delete, update and cancel are already handled inside the gridview control.
# re: adding insert capabilities to the gridview
5/11/2007 9:10 am
john
i am getting "the gridview 'x' fired event rowediting which wasn't handled" when i hit the edit linkbutton
# re: adding insert capabilities to the gridview
5/11/2007 9:48 am
john
ok, so i handled the rowediting event, but when i click on the edit linkbutton, nothing happens and the textboxes do not appear for editing. any tips? sorry for the inane questions, but i am really in a fix here.
# re: adding insert capabilities to the gridview
5/11/2007 10:47 am
bob
i need help getting the data from my accessdatasource db into the grid.can someone please post the full code (with some sort of datasource) and not just the page portion?thanks!
# re: adding insert capabilities to the gridview
5/11/2007 3:01 pm
john
my data source is a datatable that i am binding dynamically on each post-back. the rowediting event works fine, but when i hit update after editing the textbixes, the rowupdating event is not firing.
# re: adding insert capabilities to the gridview
5/15/2007 4:24 am
sauban
thanx. veru useful material. but using this code i am able to add only one row on click. but if i want to add as many as rows then what i have to do.if anyone knows plz inform me in my id sauban_kundu@rediffmail.comit will be very helpfull for me.
# re: adding insert capabilities to the gridview
5/18/2007 10:35 am
aaron
when i press insert the gridview is not updated to show the newly inserted item! how can this be accomplished?>to show the headers of the rows you will need to add them yourself. the empty data template doesn't render a table because there is no data. no table means no headers.. you are responsible for the necessary information i understand that because its an empty table its not being displayed but how exactly do you "add them yourself"? i thought possibly a table that looks like the one being generated? just a regular html table?these are really the only two issues that i am having with your code, any solutions would be great!
# re: adding insert capabilities to the gridview
6/19/2007 11:26 pm
dom
very good article - got me out a hot water - thanks dom
# re: adding insert capabilities to the gridview
7/19/2007 3:24 am
kajtek
>to show the headers of the rows you will need to add them yourself. the empty data template doesn't render a table because there is no data. no table means no headers.. you are responsible for the necessary information to add header / footer, override fields : protected gridviewrow _footerrow2; public override gridviewrow footerrow { get { gridviewrow f = base.footerrow; if (f != null) return f; else return _footerrow2; } } protected gridviewrow _headerrow2; public override gridviewrow headerrow { get { gridviewrow h = base.headerrow; if (h != null) return h; else return this._headerrow2; } }and override createchildcontrols:protected override int createchildcontrols(system.collections.ienumerable datasource, bool databinding) { int numrows = base.createchildcontrols(datasource, databinding); if (numrows == 0) { //create table table table = new table(); table.id = this.id; //convert the exisiting columns into an array and initialize datacontrolfield[] fields = new datacontrolfield[this.columns.count]; this.columns.copyto(fields, 0); _headerrow2 = base.createrow(-1, -1, datacontrolrowtype.header, datacontrolrowstate.normal); this.initializerow(_headerrow2, fields); table.rows.add(_headerrow2); //create footer row _footerrow2 = base.createrow(-1, -1, datacontrolrowtype.footer, datacontrolrowstate.normal); this.initializerow(_footerrow2, fields); this.controls.add(table); } return numrows; }
# adding datafields into gridview
9/26/2007 1:54 am
aruna
hi
# re: adding insert capabilities to the gridview
9/27/2007 7:44 pm
phild
for those that might be struggling with using a datasource, make sure you are using this.datasourcename.inputparameters[].defaultvalue and this.datasourcename.insert() to populate the input parameters and then execute, respectively. i just struggled through this example and that was what i found to be my issue.
post feedback
title:
name:
email: (never displayed)
url:
comments:
please add 6 and 6 and type the answer here:
remember me?
powered by:
skin design by mark wagner, adapted by david vidmar
copyright © ivan porto carrero
adding insert capabilities to the gridview
Précédent 434 Précédent 433 Précédent 432 Précédent 431 Précédent 430 Précédent 429 Précédent 428 Précédent 427 Précédent 426 Précédent 425 Précédent 424 Précédent 423 Précédent 422 Précédent 421 Précédent 420 Précédent 419 Précédent 418 Précédent 417 Précédent 416 Précédent 415 Précédent 414 Précédent 413 Précédent 412 Précédent 411 Précédent 410 Précédent 409 Précédent 408 Précédent 407 Précédent 406 Précédent 405 Suivant 436 Suivant 437 Suivant 438 Suivant 439 Suivant 440 Suivant 441 Suivant 442 Suivant 443 Suivant 444 Suivant 445 Suivant 446 Suivant 447 Suivant 448 Suivant 449 Suivant 450 Suivant 451 Suivant 452 Suivant 453 Suivant 454 Suivant 455 Suivant 456 Suivant 457 Suivant 458 Suivant 459 Suivant 460 Suivant 461 Suivant 462 Suivant 463 Suivant 464 Suivant 465