Solved

Bulk update on a gridview scroll through gridview

Posted on 2006-10-24
4
1,281 Views
Last Modified: 2012-08-13
I'm using the code from http://msdn2.microsoft.com/en-us/library/ms404814.aspx to do a bulk update on a datagrid. Basicly you put text boxes in the item view, then click an update button which saves the data to a table scan trhough it to see if a row has changed then does a SQL update. For some reason currentID isn't increasing each time it runs through the for loop. It runs 4 times (I have 4 row in my grid view, but the ID does not change on each time through.

    Protected Sub UpdateButton_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
        originalDataTable = CType(ViewState("originalValuesDataTable"), System.Data.DataTable)
        For Each r As GridViewRow In GridView1.Rows
            If IsRowModified(r) = True Then
                Dim currentID As Integer
                Dim currentQuantity As String
                Dim currentsellprice As String
                Dim currentUnitCost As String
                Dim currentSystemSale As Boolean
                currentID = Convert.ToInt32(GridView1.DataKeys(0).Value)
                Dim row As System.Data.DataRow = originalDataTable.Select(String.Format("OrderDetailID = {0}", currentID))(0)
                currentQuantity = CType(r.FindControl("QuantityTextBox"), TextBox).Text
                currentsellprice = CType(r.FindControl("sellpriceTextBox"), TextBox).Text
                currentUnitCost = CType(r.FindControl("UnitCostTextBox"), TextBox).Text
                currentSystemSale = CType(r.FindControl("chkSystemSale"), CheckBox).Checked
                Dim sqlcmd As String = "UPDATE OrderDetails SET Quantity = '" & currentQuantity & "', SellPrice = convert(money,'" & currentsellprice & "'), UnitCost = convert(money,'" & currentUnitCost & "'), SystemSale = '" & System.Convert.ToInt32(currentSystemSale) & "' WHERE OrderDetailID = '" & currentID & "'"
                Dim connectionString As String = WebConfigurationManager.ConnectionStrings("dbPointsConnectionString").ConnectionString
                Dim conn As New SqlConnection(connectionString)
                Dim cmd As New SqlCommand(sqlcmd, conn)
                Using conn
                    conn.Open()
                    cmd.ExecuteScalar()
                End Using
            End If
        Next
        ' Rebind the Grid to repopulate the original values table.
        tableCopied = False
        GridView1.DataBind()
    End Sub

    Protected Function IsRowModified(ByVal r As GridViewRow) As Boolean
        Dim currentID As Integer
        Dim currentQuantity As String
        Dim currentsellprice As String
        Dim currentUnitCost As String
        Dim currentSystemSale As Boolean
        currentID = Convert.ToInt32(GridView1.DataKeys(0).Value)
        currentQuantity = CType(r.FindControl("QuantityTextBox"), TextBox).Text
        currentsellprice = CType(r.FindControl("sellpriceTextBox"), TextBox).Text
        currentUnitCost = CType(r.FindControl("UnitCostTextBox"), TextBox).Text
        currentSystemSale = CType(r.FindControl("chkSystemSale"), CheckBox).Checked

        Dim row As System.Data.DataRow = originalDataTable.Select(String.Format("OrderDetailID = {0}", currentID))(0)

        Dim x As String
        x = row("sellprice")
        If Not currentQuantity.Equals(row("Quantity").ToString()) Then Return True
        If Not currentsellprice.Equals(row("sellprice").ToString()) Then Return True
        If Not currentUnitCost.Equals(row("UnitCost").ToString()) Then Return True
        If Not currentSystemSale.Equals(row("SystemSale").ToString()) Then Return True

        Return False
    End Function
0
Comment
Question by:kevlause
  • 2
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
You are always getting the key from the first row:

GridView1.DataKeys(0)

Bob
0
 

Author Comment

by:kevlause
Comment Utility
Isn't that just the first datakey as in if you name more than one datakey?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
Comment Utility
No, the DataKeys array stores the key values for each row:

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeys.aspx

Example:

Sub CustomersGridView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
     
    ' Determine the index of the selected row.
    Dim index As Integer = CustomersGridView.SelectedIndex
       
    ' Display the primary key value of the selected row.
    Message.Text = "The primary key value of the selected row is " & _
        CustomersGridView.DataKeys(index).Value.ToString() & "."
   
  End Sub

Bob
0
 

Author Comment

by:kevlause
Comment Utility
Ok that's it, but I don't understand why it works on the example on this site, but not for me. I got around it by using a session variable and increaseing it by one then setting it back to 0 when I was done.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now