1 Replies - 8396 Views - Last Post: 26 November 2011 - 01:54 PM Rate Topic: -----

#1 Nandhinimani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-November 11

how to edit data in datagridview using vb.net

Posted 25 November 2011 - 11:46 PM

hi
i have an error is Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index how to rectify this.any one help me.

Try
            If (conn.State = ConnectionState.Closed) Then
                conn.Open()
            End If
            strqry = "update tblBOMDetails set Materials='" & dgvBillMaterial.CurrentRow.Cells(2).Value & "',Weight=" & dgvBillMaterial.CurrentRow.Cells(3).Value & " where BOMDetailId='" & dgvBillMaterial.CurrentRow.Cells(1).Value & "'"

            'strqry = "update tblBOMDetails set Materials='" & dgvBillMaterial.CurrentRow.Cells(2).Value & "',Weight=" & dgvBillMaterial.CurrentRow.Cells(2).Value & " where BOMDetailId='" & dgvBillMaterial.CurrentRow.Cells(1).Value & "'"
            For i As Integer = 0 To dgvBillMaterial.Rows.Count - 1
                Materials = dgvBillMaterial.Rows(i).Cells("Materials").Value.ToString
                Weight = dgvBillMaterial.Rows(i).Cells("Weight").Value
                'BOMDetailId = dgvBillMaterial.Rows(i).Cells("BOMDetailId").Value
                strqry = "update tblBOMDetails set Materials='" & dgvBillMaterial.CurrentRow.Cells(2).Value & "',Weight=" & dgvBillMaterial.CurrentRow.Cells(3).Value & " where BOMDetailId='" & dgvBillMaterial.CurrentRow.Cells(1).Value & "'"
                cmd = New SqlCommand(strqry, conn)
                cmd.ExecuteNonQuery()
                dAdapt = New SqlDataAdapter("select * from tblBOMDetails", conn)
                dAdapt.Fill(dset, "1")
                dgvBillMaterial.DataSource = dset.Tables("1")

            Next
            btnEdit.Text = "Update"
            btnAdd.Enabled = True
            btnDelete.Enabled = True
            btnClear.Enabled = False
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            If (conn.State = ConnectionState.Open) Then
                conn.Close()
            End If
        End Try

This post has been edited by smohd: 26 November 2011 - 04:21 AM
Reason for edit:: Code tags added. Please use [code] tags when posting codes


Is This A Good Question/Topic? 0
  • +

Replies To: how to edit data in datagridview using vb.net

#2 shahan ayyub  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 25-November 11

Re: how to edit data in datagridview using vb.net

Posted 26 November 2011 - 01:54 PM

Hi!

You are modifying 'dgvBillMaterial' inside a loop without out resetting the loop count 'i'. So it may raise an error. Since 'dgvBillMaterial' rows count is controlling the loop termination.

Also when you are writing this:
dgvBillMaterial.DataSource = dset.Tables("1")
You should make sure that 'dset' contains atleast one table.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1