Solved

Bulk update on a gridview scroll through gridview

Posted on 2006-10-24
4
1,290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17797787
You are always getting the key from the first row:

GridView1.DataKeys(0)

Bob
0
 

Author Comment

by:kevlause
ID: 17797801
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
ID: 17797822
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
ID: 17798176
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

626 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