We help IT Professionals succeed at work.
Get Started

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

Last Modified: 2012-05-11
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
            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"
            'Add UPDATE Statement 
            DBCmd = New SqlCommand(sql, DBConn)

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


        Catch exp As Exception
            Label1.Text = exp.Message
        End Try
        'Close Database connection 
        DBConn = Nothing

    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" 
		<RowStyle BackColor="#EFF3FB" />
			<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 DataField="ReqID" HeaderText="Requisition" SortExpression="ReqID" ReadOnly="true" >
			<ItemStyle HorizontalAlign="Center" />
			<asp:BoundField DataField="Quantity" HeaderText="Qty" SortExpression="Quantity" >
			<ControlStyle Width="50px" />
			<ItemStyle HorizontalAlign="Right" Width="50px" />
			<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 DataField="SubTotal" DataFormatString="{0:c}" HeaderText="SubTotal" ReadOnly="true"
				HtmlEncode="False" SortExpression="SubTotal" >
			<ItemStyle HorizontalAlign="Right" />
			<asp:BoundField DataField="ModifiedBy" HeaderText="Modified By" SortExpression="ModifiedBy" 
				ReadOnly="True" >
			<ItemStyle Wrap="False" />
			<asp:BoundField DataField="DateModified" HeaderText="Date Modified" SortExpression="DateModified" 
				DataFormatString="{0:d}" HtmlEncode="False" ReadOnly="True" >
			<ItemStyle Wrap="False" HorizontalAlign="Right" />
		<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" />

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.
Watch Question
Most Valuable Expert 2012
Top Expert 2014
This problem has been solved!
Unlock 1 Answer and 29 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE