We help IT Professionals succeed at work.
Get Started

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

359 Views
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
        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.
Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
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