Items are being replace by another in the Datagridview

Posted by stephanie on Stack Overflow See other posts from Stack Overflow or by stephanie
Published on 2010-03-08T13:48:29Z Indexed on 2010/03/08 13:51 UTC
Read the original article Hit count: 215

Filed under:

When I add the first item in the datagridview its ok but when i add the second one it replace the last item being added. here's my code Private Sub add()

    Dim i As Integer
    For i = 0 To DataGridView1.Rows.Count - 1
        'DataGridView1.Rows.Add()
        DataGridView1.Rows(DataGridView1.RowCount - 1).Cells("TransID").Value = txttrans.Text
        DataGridView1.Rows(DataGridView1.RowCount - 1).Cells("ProductCode").Value = txtprodcode.Text
        DataGridView1.Rows(DataGridView1.RowCount - 1).Cells("ProductName").Value = cmbprodname.Text
        DataGridView1.Rows(DataGridView1.RowCount - 1).Cells("Quantity").Value = txtqty.Text
        DataGridView1.Rows(DataGridView1.RowCount - 1).Cells("Price").Value = txtprc.Text
        DataGridView1.Rows(DataGridView1.RowCount - 1).Cells("Amount").Value = txtat.Text
        DataGridView1.Rows(DataGridView1.RowCount - 1).Cells("CustomerName").Value = txtcust.Text
        DataGridView1.Rows(DataGridView1.RowCount - 1).Cells("Date1").Value = txtdate.Text

    Next i


End Sub

And this is in my ADDbutton:

Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click

    Try
        add()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    Dim total As Integer

    For Each row As DataGridViewRow In DataGridView1.Rows

        total += row.Cells("Amount").Value

    Next

    txtamt.Text = total

© Stack Overflow or respective owner

Related posts about vb.net