1 Replies - 8599 Views - Last Post: 21 February 2010 - 09:55 AM Rate Topic: -----

#1 Essel  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 118
  • Joined: 08-May 09

looping through datagridview

Posted 21 February 2010 - 08:07 AM

I want to use loop to insert values stored in datagrid columns into a a database table call sales but my loop is not working for as i want.
This is my code
Dim i As Integer = 0

        For i = 0 To Me.SalesDataGridView.Rows.Count - 1

            Dim connection As New SqlConnection("Data Source=ESSEL-PC;Initial Catalog=InventoryManager;Integrated Security=True")
            Try

                connection.Open()
                Dim command As New SqlCommand("Insert Into Sales ItemID,ItemName,Unitprice,Quantity,TotalPrice)Values('" & Me.txtitemid.Text & "','" & Me.txtitemname.Text & "','" & Me.txtunitprice.Text & "','" & Me.txtquantity.Text & "','" & Me.lbltotalcost.Text & "'", connection)
                command.ExecuteNonQuery()

            Catch ex As Exception

            End Try



        Next


please help me out i need to insert all values from the datagrid into the database
thank

This post has been edited by PsychoCoder: 21 February 2010 - 10:59 AM
Reason for edit:: Code tags fixed :)


Is This A Good Question/Topic? 0
  • +

Replies To: looping through datagridview

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: looping through datagridview

Posted 21 February 2010 - 09:55 AM

I think from your code that you are trying to select a row and place the columns from those rows into textboxes and then using the value of the textboxes, store that to the database.

Here's another method using datagridview bands that might be easier to follow

         For Each band As DataGridViewBand In DataGridView1.Rows
            Dim strItemID As String = CStr(DataGridView1.Rows(band.Index).Cells(0).Value)
            Dim strItemName As String = CStr(DataGridView1.Rows(band.Index).Cells(1).Value)
            Dim strUnitPrice As String = CStr(DataGridView1.Rows(band.Index).Cells(2).Value)
            Dim strQuantity As String = CStr(DataGridView1.Rows(band.Index).Cells(3).Value)
            Dim strTotalPrice As String = CStr(DataGridView1.Rows(band.Index).Cells(4).Value)
            Dim command As New SqlCommand("Insert Into Sales ItemID,ItemName,Unitprice,Quantity,TotalPrice)Values('" & strItemID & "','" & strItemName & "','" & strUnitPrice & "','" & strQuantity & "','" & strTotalPrice & "'", connection)
            command.ExecuteNonQuery()
        Next


for readability, i stored the values to variables with names similar to your fields so you can see what is happening. Basically the band in this case is each row in the datagridview. Using the for each, it iterates through the dgv and grabs the values of each column for the current row, stores them to their variables which are then used in the Insert Statement until there are no more rows

This post has been edited by CharlieMay: 21 February 2010 - 04:47 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1