user3135483
user3135483

Reputation: 13

VB.Net - DataGridView, previous entries get empty when creating new row

This is my code;

Dim TMID As Integer = -1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TMID = TMID + 1
    If TMID > 0 Then
        Form2.DataGridView1.Rows.Add()
    End If
    Form2.DataGridView1.Rows(TMID).Cells(0).Value = TMID + 1
    Form2.DataGridView1.Rows(TMID).Cells(1).Value = Me.TextBox1.Text
    Form2.DataGridView1.Rows(TMID).Cells(2).Value = "-"
    Form2.DataGridView1.Rows(TMID).Cells(3).Value = "-"
    Form2.DataGridView1.Rows(TMID).Cells(4).Value = Me.TextBox2.Text
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

End Sub

End Class

After clicking some time on Button1 (Add Button) it will look like this; https://i.sstatic.net/2iZO7.png

I'm new to VB.net :(

Upvotes: 1

Views: 325

Answers (2)

Jeff
Jeff

Reputation: 918

Check out the DataGridView.Rows property on MSDN

You can add all the items of the row at once like so

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TMID = TMID + 1
    Form2.DataGridView1.Rows.Add(TMID + 1, "test", "-", "-", "Test")
End Sub

Upvotes: 1

Ramdas Nair
Ramdas Nair

Reputation: 278

I think what you're trying to do is that every time the add button is clicked, the earlier saved data is lost. For this, I believe, save the data to a database, on each click on add button. In the Form2.DataGridView1, add the data from the database. :)

Upvotes: 0

Related Questions