
Originally Posted by
BONITO
hi Sir Jm,
thanks for that comment !
I try 3 codes and the last one works !
Code:
' dgvCell1_Muscatel_MPCA.Rows.Clear()
'dgvCell1_Muscatel_MPCA.DataSource = Nothing
dgvCell1_Muscatel_MPCA.DataSource.clear()
So did you think about why each of those works or doesn't work? If the grid is bound then it displays the contents of its DataSource. The first one can't work because you can't add or remove rows directly in a bound grid. The second one doesn't help because you simply bind the same DataTable again after loading more rows into it so you never actually remove the old rows. The last one works because the DataSource of the grid is the DataTable that contains the rows so you actually are removing those old rows. It's still not really ideal though, given that it's relying on late-binding.
Here's where you're actually populating the grid:
Code:
If dr_C2.HasRows Then
dtRecords_C2.Load(dr_C2)
dgvCell2_Muscaatel_MPCA.DataSource = dtRecords_C2
Else
MsgBox("NO DATA ", MsgBoxStyle.Critical, "ERROR")
' txtEfficiency.Text = Efficiency.ToString("p2")
dr_C2.Close()
End If
Why not simply add a line above that to Clear the Rows of the DataTable before you populate it?
Code:
dtRecords_C2.Rows.Clear()
If dr_C2.HasRows Then
dtRecords_C2.Load(dr_C2)
dgvCell2_Muscaatel_MPCA.DataSource = dtRecords_C2
Else
MsgBox("NO DATA ", MsgBoxStyle.Critical, "ERROR")
' txtEfficiency.Text = Efficiency.ToString("p2")
dr_C2.Close()
End If
Simple. If you don't want to keep any old rows in the DataTable when adding new ones then just remove the old ones before you add the new ones.