asp.net - How to add new row to a databound gridview? -
i'm trying add new blank row gridview whenever user clicks add row button. ideally, whichever row click button, new row inserted below row. i've read through lot of previous questions still cannot gridview display new blank row.
page code:
<table> ... ... <td colspan="3" align="left" style="border: thin solid #000000; vertical-align: top; background-color: #4b6c9e"> <asp:updatepanel id="updatepanel1" runat="server" > <contenttemplate> <asp:gridview id="grdhours" runat="server" autogeneratecolumns="false" width="100%" cellpadding="4" forecolor="#333333" gridlines="none" > <alternatingrowstyle backcolor="white" verticalalign="top" height="20px"/> <columns> <asp:templatefield showheader="false" headertext="task"> <itemtemplate> <asp:label id="bufferlabel" runat="server" text="" visible="false"></asp:label> <asp:textbox id="tasklist" runat="server" cssclass="mydropdownsearch" width="80%" onfocus="inputfocus(this)" onblur="inputblur(this)" value="select task..."></asp:textbox> <asp:autocompleteextender servicemethod="searchemployees" minimumprefixlength="3" completioninterval="100" enablecaching="false" completionsetcount="10" targetcontrolid="tasklist" id="autocompleteextender3" runat="server" firstrowselected = "false"> </asp:autocompleteextender> </itemtemplate> <itemstyle width="15%" /> </asp:templatefield> <asp:boundfield datafield="weekday" headertext="weekday" > <headerstyle horizontalalign="left" /> <itemstyle horizontalalign="left" width="4%" /> </asp:boundfield> <asp:boundfield datafield="date" dataformatstring="{0:d}" headertext="date" > <headerstyle horizontalalign="center" /> <itemstyle horizontalalign="center" width="8%" /> </asp:boundfield> <asp:templatefield headertext="hours"> <itemtemplate> <asp:label id="lblhours" visible='<%# not isineditmode %>' runat="server" text='<%# eval("hours") %>' cssclass="hourslabel" /> <asp:textbox id="textbox1" runat="server" text='<%# eval("hours") %>' width="90%" visible='<%# isineditmode %>'> </asp:textbox> </itemtemplate> <headerstyle horizontalalign="center" /> <itemstyle width="5%" horizontalalign="center" /> </asp:templatefield> <asp:templatefield headertext="minutes"> <itemtemplate> <asp:label id="lblmins" visible='<%# not isineditmode %>' runat="server" text='<%# eval("minutes") %>' cssclass="hourslabel" /> <asp:dropdownlist id="dropdownlist2" runat="server" cssclass="mydropdown" selectedvalue='<%# eval("minutes") %>' visible='<%# isineditmode %>' > <asp:listitem></asp:listitem> <asp:listitem>0</asp:listitem> <asp:listitem>15</asp:listitem> <asp:listitem>30</asp:listitem> <asp:listitem>45</asp:listitem> </asp:dropdownlist> </itemtemplate> <headerstyle horizontalalign="center" /> <itemstyle width="5%" horizontalalign="center" /> </asp:templatefield> <asp:templatefield headertext="activity" > <itemtemplate> <asp:label id="lblactivity" visible='<%# not isineditmode %>' runat="server" text='<%# eval("activity") %>' cssclass="hourslabel" /> <asp:dropdownlist id="dropdownlist1" runat="server" width="75%" selectedvalue='<%# eval("activity") %>' visible='<%# isineditmode %>'> <asp:listitem selected="true">select activity</asp:listitem> <asp:listitem></asp:listitem> <asp:listitem>prescope</asp:listitem> <asp:listitem>scope</asp:listitem> <asp:listitem>design/build configuration</asp:listitem> <asp:listitem>testing</asp:listitem> <asp:listitem>training</asp:listitem> <asp:listitem>implementation/validation</asp:listitem> <asp:listitem>maintenance & ongoing support</asp:listitem> <asp:listitem>project management</asp:listitem> <asp:listitem>shared time</asp:listitem> </asp:dropdownlist> <asp:requiredfieldvalidator id="requiredfieldvalidator1" runat="server" errormessage="*" visible="false" controltovalidate="dropdownlist1"></asp:requiredfieldvalidator> </itemtemplate> <headerstyle horizontalalign="center" /> <itemstyle width="20%" horizontalalign="center" /> </asp:templatefield> <asp:templatefield showheader="false" headertext="addentries"> <itemtemplate> <asp:button id="btnhoursrow" runat="server" text="+" visible='<%# isineditmode %>' onclick="btnnewentryrow_click" /> </itemtemplate> <itemstyle width="5%" /> </asp:templatefield> <asp:templatefield showheader="false" headertext="addnotes"> <itemtemplate> <asp:label id="lblnotes" visible='<%# not isineditmode %>' runat="server" cssclass="hourslabel" /> <asp:button id="btnaddnotes" runat="server" text="add notes" visible='<%# isineditmode %>' /> <asp:modalpopupextender id="btnaddnotes_modalpopupextender" runat="server" dynamicservicepath="" enabled="true" popupcontrolid="pnlnotes" targetcontrolid="btnaddnotes" okcontrolid="btnnotesdone"> </asp:modalpopupextender> </itemtemplate> <itemstyle width="5%" /> </asp:templatefield> <asp:templatefield showheader="false" headertext="validator"> <itemtemplate> <asp:regularexpressionvalidator id="regularexpressionvalidator1" runat="server" errormessage="whole numbers only" controltovalidate="textbox1" validationgroup="hoursasintegers" validationexpression="\d+" setfocusonerror="false" width="75%" font-bold="true" forecolor="red" display="dynamic"></asp:regularexpressionvalidator> </itemtemplate> <itemstyle width="10%" /> </asp:templatefield> <asp:templatefield showheader="false" headertext="errmessage"> <itemtemplate> <asp:label id="activityerrormessage" runat="server" text="you must select activity entered hours." visible="false" forecolor="#ff0000" font-bold="true"></asp:label> </itemtemplate> <itemstyle width="19%" /> </asp:templatefield> <asp:templatefield headertext="timenotes"> <itemtemplate> <asp:panel id="pnlnotes" runat="server" visible='<%# isineditmode %>' width="75%" backcolor="#4b6c9e" height="300px"> <table width="100%"> <tr> <td> <asp:label id="label1" runat="server" text="enter notes below:" forecolor="white" font-size="medium"></asp:label> </td> </tr> <tr> <td align="center"> <asp:textbox id="txtnotes" runat="server" cssclass="mytextbox" textmode="multiline" text='<%# eval("timenotes") %>' height="200"></asp:textbox> </td> </tr> <tr> <td> <asp:button id="btnnotesdone" runat="server" text="done" /> <asp:button id="btnnotescancel" runat="server" text="cancel" onclick="btnnotescancel_click" visible="false" /> </td> </tr> </table> </asp:panel> </itemtemplate> <itemstyle width="4%" /> </asp:templatefield> </columns> <editrowstyle backcolor="#2461bf" /> <footerstyle backcolor="white" font-bold="true" forecolor="white" /> <headerstyle backcolor="#507cd1" font-bold="true" forecolor="white" /> <pagerstyle backcolor="#2461bf" forecolor="white" horizontalalign="center" /> <rowstyle backcolor="#eff3fb" height="20px" verticalalign="top" /> <selectedrowstyle backcolor="#d1ddf1" font-bold="true" forecolor="#333333" /> <sortedascendingcellstyle backcolor="#f5f7fb" /> <sortedascendingheaderstyle backcolor="#6d95e1" /> <sorteddescendingcellstyle backcolor="#e9ebef" /> <sorteddescendingheaderstyle backcolor="#4870be" /> </asp:gridview> </contenttemplate> </asp:updatepanel> </td>
code behind:
protected sub btnnewentryrow_click(byval sender object, byval e eventargs) ' dim clickedrow = trycast(directcast(sender, button).namingcontainer, gridviewrow) ' dim clickedindex = clickedrow.rowindex 'just hard coded value bindgrdhours(3) end sub private sub bindgrdhours(byval rowindex integer) dim rowindex integer = 0 if viewstate("currentgridtable") isnot nothing dim dtcurrenttable datatable = directcast(viewstate("currentgridtable"), datatable) dim drcurrentrow datarow = nothing if dtcurrenttable.rows.count > 0 integer = 1 dtcurrenttable.rows.count dim tasklist textbox = directcast(grdhours.rows(rowindex).cells(1).findcontrol("tasklist"), textbox) 'dim textboxage boundfield 'textboxage.datafield = grdhours.rows(rowcount).cells(2).findcontrol("weekday").tostring 'dim textboxaddress boundfield 'textboxaddress.datafield = grdhours.rows(rowcount).cells(3).findcontrol("date").tostring dim hours textbox = directcast(grdhours.rows(rowindex).cells(4).findcontrol("textbox1"), textbox) dim minutes dropdownlist = directcast(grdhours.rows(rowindex).cells(5).findcontrol("dropdownlist2"), dropdownlist) dim activity dropdownlist = directcast(grdhours.rows(rowindex).cells(6).findcontrol("dropdownlist1"), dropdownlist) dim addentries button = directcast(grdhours.rows(rowindex).cells(7).findcontrol("btnhoursrow"), button) drcurrentrow = dtcurrenttable.newrow() ' drcurrentrow("rownumber") = + 1 dtcurrenttable.rows(i - 1)("task") = tasklist.text.tostring dtcurrenttable.rows(i - 1)("weekday") = grdhours.rows(rowindex).cells(2).text.tostring dtcurrenttable.rows(i - 1)("date") = grdhours.rows(rowindex).cells(3).text.tostring dtcurrenttable.rows(i - 1)("hours") = hours.text.tostring dtcurrenttable.rows(i - 1)("minutes") = minutes.selectedvalue.tostring dtcurrenttable.rows(i - 1)("activity") = activity.selectedvalue.tostring dtcurrenttable.rows(i - 1)("addentries") = addentries.text.tostring dtcurrenttable.rows(i - 1)("addnotes") = string.empty dtcurrenttable.rows(i - 1)("validator") = string.empty dtcurrenttable.rows(i - 1)("errmessage") = string.empty dtcurrenttable.rows(i - 1)("timenotes") = string.empty rowindex += 1 next dtcurrenttable.rows.add(drcurrentrow) viewstate("currentgridtable") = dtcurrenttable grdhours.datasource = dtcurrenttable grdhours.databind() dim txn textbox = directcast(grdhours.rows(rowindex).cells(1).findcontrol("textbox1"), textbox) ' txn.focus; txn.focus() end if end if enterhours(true) end sub
when click button, btnnewentryrow_click function being called, in turns calls bindgrdhours() after completion, nothing happening. screen still contains original gridview.
any ideas on i'm going wrong? looking.
Comments
Post a Comment