utlonghornjulie
asked on
ASP:Gridview bulk edit/update fields
I have a gridview on my web page that has two columns that are editable. These two fields are editable for all rows at the same time. How would I update all the fields that have been changed on the webpage using one update statement when the user clicks an update button? Right now I have an ontextchanged event on each textbox on the gridview. When the text is changed, I have a sub that should run called TextBox_TextChanged. Right now the TextBox_TextChanged event never fires when I change the field.
<asp:GridView ID="dgRateDetailUpdate" runat="server" AutoGenerateColumns="False"
DataKeyNames="RC_RateDetID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="RC_RateDetID" HeaderText="RC_RateDetID" InsertVisible="False"
ReadOnly="True" SortExpression="RC_RateDetID" />
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lblRCRateDetID" Text='<%# DataBinder.Eval(Container.DataItem,"RC_RateDetID") %>'
runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="JS_ActBillCalcID" HeaderText="JS_ActBillCalcID" SortExpression="JS_ActBillCalcID" />
<asp:BoundField DataField="RC_ActBillCalcID" HeaderText="RC_ActBillCalcID" SortExpression="RC_ActBillCalcID" />
<asp:BoundField DataField="RC_CreateDate" HeaderText="RC_CreateDate" SortExpression="RC_CreateDate" />
<asp:BoundField DataField="CustNo" HeaderText="CustNo" SortExpression="CustNo" />
<asp:BoundField DataField="RateDetID" HeaderText="RateDetID" SortExpression="RateDetID" />
<asp:BoundField DataField="RateID" HeaderText="RateID" SortExpression="RateID" />
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" />
<asp:BoundField DataField="RateTypeID" HeaderText="RateTypeID" SortExpression="RateTypeID" />
<asp:BoundField DataField="ConsUnitID" HeaderText="ConsUnitID" SortExpression="ConsUnitID" />
<asp:BoundField DataField="RateDescID" HeaderText="RateDescID" SortExpression="RateDescID" />
<asp:BoundField DataField="EffectiveDate" HeaderText="EffectiveDate" SortExpression="EffectiveDate" />
<asp:BoundField DataField="ExpirationDate" HeaderText="ExpirationDate" SortExpression="ExpirationDate" />
<asp:BoundField DataField="RateAmt" HeaderText="RateAmt" SortExpression="RateAmt" />
<asp:BoundField DataField="RateAmt2" HeaderText="RateAmt2" SortExpression="RateAmt2" />
<asp:BoundField DataField="RateAmt3" HeaderText="RateAmt3" SortExpression="RateAmt3" />
<asp:BoundField DataField="FixedAdder" HeaderText="FixedAdder" SortExpression="FixedAdder" />
<asp:BoundField DataField="MinDetAmt" HeaderText="MinDetAmt" SortExpression="MinDetAmt" />
<asp:BoundField DataField="MaxDetAmt" HeaderText="MaxDetAmt" SortExpression="MaxDetAmt" />
<asp:BoundField DataField="GLAcct" HeaderText="GLAcct" SortExpression="GLAcct" />
<asp:BoundField DataField="RangeLower" HeaderText="RangeLower" SortExpression="RangeLower" />
<asp:BoundField DataField="RangeUpper" HeaderText="RangeUpper" SortExpression="RangeUpper" />
<asp:BoundField DataField="CustType" HeaderText="CustType" SortExpression="CustType" />
<asp:BoundField DataField="Graduated" HeaderText="Graduated" SortExpression="Graduated" />
<asp:BoundField DataField="Progressive" HeaderText="Progressive" SortExpression="Progressive" />
<asp:BoundField DataField="AmountCap" HeaderText="AmountCap" SortExpression="AmountCap" />
<asp:BoundField DataField="MaxRateAmt" HeaderText="MaxRateAmt" SortExpression="MaxRateAmt" />
<asp:BoundField DataField="MinRateAmt" HeaderText="MinRateAmt" SortExpression="MinRateAmt" />
<asp:BoundField DataField="CategoryRollup" HeaderText="CategoryRollup" SortExpression="CategoryRollup" />
<asp:BoundField DataField="Taxable" HeaderText="Taxable" SortExpression="Taxable" />
<asp:BoundField DataField="ChargeType" HeaderText="ChargeType" SortExpression="ChargeType" />
<asp:BoundField DataField="MiscData1" HeaderText="MiscData1" SortExpression="MiscData1" />
<asp:BoundField DataField="FixedCapRate" HeaderText="FixedCapRate" SortExpression="FixedCapRate" />
<asp:BoundField DataField="ScaleFactor1" HeaderText="ScaleFactor1" SortExpression="ScaleFactor1" />
<asp:BoundField DataField="ScaleFactor2" HeaderText="ScaleFactor2" SortExpression="ScaleFactor2" />
<asp:TemplateField HeaderText="ViterraPickup" SortExpression="ViterraPickup">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ViterraPickup") %>' OnTextChanged="TextBox_TextChanged"></asp:TextBox>
<asp:CompareValidator runat="server" ID="cvViterraPickup" ControlToValidate="TextBox3"
ErrorMessage="Must be integer, and can not be 0." ValueToCompare="0" Type="Integer" Operator="NotEqual"></asp:CompareValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ISTA_PickupDate" HeaderText="ISTA_PickupDate" SortExpression="ISTA_PickupDate" />
<asp:BoundField DataField="ISTA_Notes" HeaderText="ISTA_Notes" SortExpression="ISTA_Notes" />
<asp:TemplateField HeaderText="GM_Notes" SortExpression="GM_Notes">
<ItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("GM_Notes") %>' OnTextChanged="TextBox_TextChanged"
></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="RC_Note" HeaderText="RC_Note" SortExpression="RC_Note" />
<asp:BoundField DataField="RC_Status" HeaderText="RC_Status" SortExpression="RC_Status" />
<asp:BoundField DataField="PremNo" HeaderText="PremNo" SortExpression="PremNo" />
<asp:BoundField DataField="ISTA_RateDetID" HeaderText="ISTA_RateDetID" SortExpression="ISTA_RateDetID" />
<asp:BoundField DataField="PremID" HeaderText="PremID" SortExpression="PremID" />
</Columns>
<HeaderStyle BackColor="#04489D" Font-Bold="true" Font-Size="10pt" ForeColor="white"
Font-Names="Arial" VerticalAlign="top" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:StikeData %>"
SelectCommand="SELECT * FROM [RateDetail_Update] WHERE (([ViterraPickup] <> @ViterraPickup) AND ([ViterraPickup] <> @ViterraPickup2) AND ([ViterraPickup] <> @ViterraPickup3) AND ([ViterraPickup] <> @ViterraPickup4) AND ([ViterraPickup] <> @ViterraPickup5) AND ([ViterraPickup] <> @ViterraPickup6))">
<SelectParameters>
<asp:Parameter DefaultValue="0" Name="ViterraPickup" Type="Int32" />
<asp:Parameter DefaultValue="9999" Name="ViterraPickup2" Type="Int32" />
<asp:Parameter DefaultValue="4" Name="ViterraPickup3" Type="Int32" />
<asp:Parameter DefaultValue="99" Name="ViterraPickup4" Type="Int32" />
<asp:Parameter DefaultValue="5555" Name="ViterraPickup5" Type="Int32" />
<asp:Parameter DefaultValue="6666" Name="ViterraPickup6" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Public rowChanged() As Boolean
Protected Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisTextBox As TextBox = sender
Dim row As Integer
Dim thisGridViewRow As GridViewRow
thisGridViewRow = thisTextBox.Parent.Parent
row = thisGridViewRow.RowIndex
rowChanged(row) = True
End Sub
Protected Sub btnSaveRateDetails_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveRateDetails.Click
'If Page.IsPostBack Then
Dim totalRows As Integer = dgRateDetailUpdate.Rows.Count
Dim r As Integer
Dim SRCommand1 As New System.Data.SqlClient.SqlCommand()
Dim SQL_RateDetailUpdate As String
Dim SRConn1 As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("StikeData").ConnectionString)
If SRConn1.State <> Data.ConnectionState.Open Then
SRConn1.Open()
End If
For r = 0 To totalRows
If rowChanged(r) = True Then
Dim thisGridViewRow As GridViewRow
thisGridViewRow = dgRateDetailUpdate.Rows(r)
Dim lbl1 As Label
lbl1 = thisGridViewRow.FindControl("lblRCRateDetID")
Dim pk As String = lbl1.Text
Dim tb1 As TextBox
tb1 = thisGridViewRow.FindControl("TextBox3")
Dim cell1 As String
cell1 = tb1.Text
Dim tb2 As TextBox
tb2 = thisGridViewRow.FindControl("TextBox4")
Dim cell2 As String
cell2 = tb2.Text
SQL_RateDetailUpdate = " UPDATE dbo.RateDetailUpdate SET " _
& " ViterraPickup = " & CInt(cell1) & ", GM_Notes = '" & cell2 & "' " _
& " WHERE RC_RateDetID = " & CInt(pk) & "; "
SRCommand1.CommandTimeout = 300
SRCommand1.Connection = SRConn1
SRCommand1.CommandText = SQL_RateDetailUpdate
SRCommand1.ExecuteNonQuery()
End If
r = r + 1
Next
dgRateDetailUpdate.DataBind()
If SRConn1.State <> Data.ConnectionState.Closed Then
SRConn1.Close()
End If
'End If
End Sub
Well, you could set your update command in the same datasource the feeds the gridview. Then place a save all button somewhere on the page that calls an update function in the code behind.
Hope this helps,
Dustin
Hope this helps,
Dustin
'Update function
Sub update()
dim ind as integer
dim row as GridViewRow
for each row in dgRateDetailUpdate.Rows
ind = row.RowIndex
dgRateDetailUpdate.updaterow(ind,false)
next
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For the second question, I am copying the following from one of my earlier posts:
Further, as per my knowledge there are events such As TextChanged which are not fired automatically (means they don't create postback from client), although when some other control having postback functionality such as a button will create a postback, the TextChanged property of the textbox will be executed.
Although not related to Textbox but Hyperlink but the same could be done for Textbox by using a hidden button, I have answered on relevent but not exact question to use such as trick. Please see here:
[i]http://www.experts-exch