Edit many rows in a Data Grid

VBAHole22

Contributor
Joined
Oct 21, 2003
Messages
432
Location
VA
Is it possible to have a data grid that has, say, 25 rows of data in it and let the user update all of the cells and then click submit and have the back end table updated?

I don't want the user to have to click on an edit column and then click in the cell they want to edit and then click on the update link. Seems like a lot of clicking.

Is it possible to do this?
Right now I'm using a datagrid in vb.net
 
Yes, it's possible.

I use ONE <ItemTemplate>, have your rows in there, then you'll have one Save/Update button and there u go.
 
Can that really work?
How do I go about generating the Update SQL statements? Or does .NET do that for me?
Right now I am using a datagrid with an Edit Command Column.
 
It does work, I'm coding a section now:

I'm doing it like this:

Code:
<asp:datagrid id="dgSiteAddInfoBottom" runat="server" AutoGenerateColumns="False" OnEditCommand="SiteAddInfo_Edit" OnCancelCommand="SiteAddInfo_Cancel" OnUpdateCommand="SiteAddInfo_Update" cssclass="tblSiteAddInfoBottom2">
					<AlternatingItemStyle BackColor="White" Height="0"></AlternatingItemStyle>
					<Columns>
						<asp:TemplateColumn>
							<HeaderStyle></HeaderStyle>
							<ItemTemplate>
								<table border="0">
									<tr>
										<td align="left" style="font-size:small;"><b>Change Log:</b></td>
										<td width="100px" style="font-size:small;">
											<%# Container.DataItem("other_string1") %>
										</td>
										
									</tr>
									<tr>
										<td align="left" style="font-size:small;"><b>Change Log:</b></td>
										<td style="font-size:small;">
											<%# Container.DataItem("other_string2") %>
										</td>
										
									</tr>
									<tr>
										<td align="left" style="font-size:small;"><b>Old Change Log:</b></td>
										<td style="font-size:small;">
											<%# Container.DataItem("other_string3") %>
										</td>
										
									</tr>
									<tr>
										<td align="left" style="font-size:small;"><b>Related Incidents:</b></td>
										<td style="font-size:small;">
											<%# Container.DataItem("other_string4") %>
										</td>
									
									</tr>
									<tr>
										<td align="left" style="font-size:small;"><b>Billable (Y/N):</b></td>
										<td style="font-size:small;">
											<%# Container.DataItem("other_string5") %>
										</td>
									
									</tr>
									<tr>
										<td align="left" style="font-size:small;"><b>Billable Rate:</b></td>
										<td style="font-size:small;">
											<%# Container.DataItem("other_string6") %>
										</td>
										
									</tr>
									<tr>
										<td align="left" style="font-size:small;"><b>Billable Hours:</b></td>
										<td style="font-size:small;">
											<%# Container.DataItem("other_string7") %>
										</td>
										
									</tr>
									<tr>
										<td align="left" style="font-size:small;"><b>Billable PO#:</b></td>
										<td style="font-size:small;">
											<%# Container.DataItem("other_string8") %>
										</td>
										
									</tr>
									<tr>
										<td align="left" style="font-size:small;"><b>SP Time:</b></td>
										<td style="font-size:small;">
											<%# Container.DataItem("other_string10") %>
										</td>
										
									</tr>
									<tr>
									</tr>
									<tr>
									</tr>
								</table>
							</ItemTemplate>
							<EditItemTemplate>
								<table border="0">
									<tr>
										<td align="left" style="font-size:small;"><b>Change Log:</b></td>
										<td width="100px" style="font-size:small;">
											<asp:Textbox id="txtOString1" runat="server" MaxLength=50 Text='<%# Container.DataItem("other_string1") %>' >
											</asp:Textbox>
										</td>
										
									</tr>
									<tr>
										<td align="left" style="font-size:small;"><b>Change Log:</b></td>
										<td style="font-size:small;">
											<asp:Textbox id="txtOstring2" runat="server" MaxLength=50 Text='<%# Container.DataItem("other_string2") %>'>
											</asp:Textbox>
										</td>
										
									</tr>
									<tr>
										<td align="left" style="font-size:small;"><b>Old Change Logs:</b></td>
										<td style="font-size:small;">
											<asp:Textbox id="txtOString3" runat="server" MaxLength=50 Text='<%# Container.DataItem("other_string3") %>'>
											</asp:Textbox>
										</td>
									
									</tr>
									<tr>
										<td align="left" style="font-size:small;"><b>Related Incidents:</b></td>
										<td style="font-size:small;">
											<asp:Textbox id="txtOString4" runat="server" MaxLength=50 Text='<%# Container.DataItem("other_string4") %>'>
											</asp:Textbox>
										</td>
										
									</tr>
									<tr>
										<td align="left" style="font-size:small;"><b>Billable (Y/N):</b></td>
										<td style="font-size:small;">
											<asp:Textbox id="txtOString5" runat="server" MaxLength=50 Text='<%# Container.DataItem("other_string5") %>'>
											</asp:Textbox>
										</td>
										
									</tr>
									<tr>
										<td align="left" style="font-size:small;"><b>Billable Rate:</b></td>
										<td style="font-size:small;">
											<asp:Textbox id="txtOString6" runat="server" MaxLength=50 Text='<%# Container.DataItem("other_string6") %>'>
											</asp:Textbox>
										</td>
										
									</tr>
									<tr>
										<td align="left" style="font-size:small;"><b>Billable Hours:</b></td>
										<td style="font-size:small;">
											<asp:Textbox id="txtOString7" runat="server" MaxLength=50 Text='<%# Container.DataItem("other_string7") %>'>
											</asp:Textbox>
										</td>
										
									
									</tr>
									<tr>
										<td align="left" style="font-size:small;"><b>Billable PO#:</b></td>
										<td style="font-size:small;">
											<asp:Textbox id="txtOString8" runat="server" MaxLength=50 Text='<%# Container.DataItem("other_string8") %>'>
											</asp:Textbox>
										</td>
										
									</tr>
									<tr>
										<td align="left" style="font-size:small;"><b>SP Time:</b></td>
										<td style="font-size:small;">
											<asp:Textbox id="txtOString10" runat="server" MaxLength=50 Text='<%# Container.DataItem("other_string10") %>' >
											</asp:Textbox>
										</td>
										
									</tr>
								</table>
							</EditItemTemplate>
						</asp:TemplateColumn>
						<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Save" CancelText="Cancel" EditText="Edit">
							<HeaderStyle></HeaderStyle>
						</asp:EditCommandColumn>
					</Columns>
				</asp:datagrid>

Then in code behind, in datagrid's update routine:

Code:
Dim strO1 As String = CType(e.Item.FindControl("txtOString1"), TextBox).Text
        Dim strO2 As String = CType(e.Item.FindControl("txtOstring2"), TextBox).Text
 
 ....

 I call Stored Proc and save
 
So do you loop through each record and then build parameters for your stored proc that has update statements in it?

Do you have a way to know which rows have been updated or do you send them all?

I suppose I could create a loop that would pick out the values in each cell for each row and then generate SQL along the way like so:

for r = 0 to dg.rows
for c=0 to dg.columns
value(r,c) = CType(e.Item.Cells(c).Controls(0), System.Web.UI.webControls.TextBox.Text)
dim strSQL as String = "Update table Set x = value(r,c)


Something like that? Seems like a lot of heavy lifting for .NOT

Is there an easier way?
 
1. "or do you send them all?" .. I send them all because I dont care if all get updated.

2. "Do you have a way to know which rows have been updated".. I had the same question once and You might find the answer if u do a search on this forum. There's a way to do it and someone posted a sample code.

One way to do it, is the first time you bind the data, hold the original values in a session var (or maybe view state). When you want to update, compare the orig value to the new value use entered.

But then, someone told me why bother, just update the whole thing.
 
Back
Top