Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Bulk update on a gridview scroll through gridview

Posted on 2006-10-24
4
Medium Priority
?
1,293 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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

705 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