troubleshooting Question

Trigger a GridView Update when a value needs to be changed programatically

Avatar of David Megnin
David MegninFlag for United States of America asked on
ASP.NETVisual Basic.NET.NET Programming
29 Comments1 Solution362 ViewsLast Modified:
I have two GridViews in a Master/Detail arrangement.  It's a Purchase Requisition form where the main information is in GridVew1 and the Items are in GridView2.  On this page if you Select a Purchase Requisition record in GridView1 then GridView2 displays all the Items associated with that PR.

I'm trying to add a Button to VOID out a PR by setting the Quantity of all the Items to zero (0).  I started by having the button just update the Quantity field in the database setting it to zero, but that does not trigger GridView2 to update its subtotals and update the grand total in GridView1.

Just doing what I have so far does update the Quantity in GridView2, but leaves the subtotals unchanged.  If I click Edit and then Update (Save) on the Item in GridView2 it updates everything like it should.

I put GridView2.DataBind() at the end of the Void Button click event, but it doesn't trigger the Update that makes all the calculations happen, it just updates the one Quantity field.

Here is the current button click:
    Protected Sub btnVoid_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoid.Click
        Dim DBConn As New SqlConnection(ConfigurationManager.ConnectionStrings("PurchaseReqConnectionString").ConnectionString)
        Dim DBCmd As New SqlCommand
        Try
            Dim row As GridViewRow = Me.GridView1.SelectedRow
            Dim vReqID As Integer = row.Cells(1).Text

            Dim sql As String
            sql = "UPDATE Items SET Quantity = 0 WHERE ReqID = @ReqID"  'ItemID = @ItemID and ReqID = @ReqID"
            DBConn.Open()
            'Add UPDATE Statement 
            DBCmd = New SqlCommand(sql, DBConn)

            'Add Database Parameters
            DBCmd.Parameters.Add("@ReqID", SqlDbType.Int).Value = vReqID

            DBCmd.ExecuteNonQuery()

        Catch exp As Exception
            Response.Write(exp.Message)
            Label1.Text = exp.Message
        End Try
        'Close Database connection 
        DBCmd.Dispose()
        DBConn.Close()
        DBConn = Nothing

        GridView2.DataBind()
    End Sub

Open in new window


Here is GridView2:
	<asp:GridView ID="GridView2" runat="server" AllowSorting="True" AutoGenerateColumns="False" 
		CellPadding="4" DataKeyNames="ItemID" DataSourceID="SqlDataSource2" ForeColor="#333333" 
		ShowFooter="True">
		<RowStyle BackColor="#EFF3FB" />
		<Columns>
			<asp:CommandField ShowEditButton="True" UpdateText="Save" />
			<asp:BoundField DataField="ItemID" HeaderText="ItemID" InsertVisible="False" ReadOnly="True" 
				SortExpression="ItemID" Visible="False" />
			<asp:BoundField DataField="ItemNumber" HeaderText="Item #" SortExpression="ItemNumber" ReadOnly="true" >
			<ItemStyle HorizontalAlign="Center" />
			</asp:BoundField>
			<asp:BoundField DataField="ReqID" HeaderText="Requisition" SortExpression="ReqID" ReadOnly="true" >
			<ItemStyle HorizontalAlign="Center" />
			</asp:BoundField>
			<asp:BoundField DataField="Quantity" HeaderText="Qty" SortExpression="Quantity" >
			<ControlStyle Width="50px" />
			<ItemStyle HorizontalAlign="Right" Width="50px" />
			</asp:BoundField>
			<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
			<asp:BoundField DataField="UnitPrice" DataFormatString="{0:c}" HeaderText="Unit Price" ApplyFormatInEditMode="false"
				HtmlEncode="False" SortExpression="UnitPrice" >
			<ControlStyle Width="50px" />
			<ItemStyle HorizontalAlign="Right" Width="50px" />
			</asp:BoundField>
			<asp:BoundField DataField="SubTotal" DataFormatString="{0:c}" HeaderText="SubTotal" ReadOnly="true"
				HtmlEncode="False" SortExpression="SubTotal" >
			<ItemStyle HorizontalAlign="Right" />
			</asp:BoundField>
			<asp:BoundField DataField="ModifiedBy" HeaderText="Modified By" SortExpression="ModifiedBy" 
				ReadOnly="True" >
			<ItemStyle Wrap="False" />
			</asp:BoundField>
			<asp:BoundField DataField="DateModified" HeaderText="Date Modified" SortExpression="DateModified" 
				DataFormatString="{0:d}" HtmlEncode="False" ReadOnly="True" >
			<ItemStyle Wrap="False" HorizontalAlign="Right" />
			</asp:BoundField>
		</Columns>
		<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
		<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
		<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
		<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
		<EditRowStyle BackColor="#FF99FF" />
		<AlternatingRowStyle BackColor="White" />
	</asp:GridView>

Open in new window


I hope all I need to do is to somehow trigger GridView2 to do an Edit/Update cycle.  Thanks.
Oh, if it's relevant the number of items could be anything from 1 to 10.
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 29 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 29 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros