How to clear two different datagridview at the same time

PRAISE PHS

Well-known member
Joined
Jun 2, 2011
Messages
58
Programming Experience
Beginner
Hi All,
Pls, kindly help me with this:
I fill two different datagridview with items on my app. But when I try to clear the gridview by using dataset.clear, one of the gridview is cleared while the second gridview remains uncleared. How do I clear the two grids simultaneously by using a dataset. Below are the code I used to populate the grids.

VB.NET:
Private Sub btnGoodPart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGoodPart.Click
 Adapter = New SqlDataAdapter("SELECT PartName FROM PartDetails WHERE Condition = 1 AND MachineID = '" & Integer.Parse(arst) & "'", MyConn)
            scb = New SqlCommandBuilder
            ds = New DataSet
            Adapter.Fill(ds, "PartDetails")


            DataGridViewGood.DataSource = ds
            DataGridViewGood.DataMember = ("PartDetails")
End Sub


Private Sub btnBadPart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBadPart.Click
 Adapter = New SqlDataAdapter("SELECT PartName FROM PartDetails WHERE Condition = 0 AND MachineID = '" & Integer.Parse(arst) & "'", MyConn)
            scb = New SqlCommandBuilder
            ds = New DataSet
            Adapter.Fill(ds, "PartDetails")


            DataGridViewBad.DataSource = ds
            DataGridViewBad.DataMember = ("PartDetails")
End Sub
 
Think about this. Let's say that you and I are flatmates and we have a cupboard in which we keep are going to keep a container of cornflakes. You buy a new container, fill it with cornflakes and put it in the cupboard. I then buy a new container, fill it with cornflakes and put it in the cupboard, throwing yours aside when I do so. If I then empty the container that I bought, would you expect the container that you bought to magically empty as well? Of course not, so why are you expecting that same magic to happen in your app?

In your app, you are creating two DataSets. If you clear one, why would the other magically clear too? You're also creating two data adapters and two command builders. Is that really what you want? That said, you're filling a DataTable with the same name each time so, if you use one DataSet, you're going to end up with all the data in one DataTable. You should probably be using two DataTables. You can put them in the same DataSet or not use a DataSet at all. Also, if you're going to use two data adapters and two command builders then you should have two variables of each type as well. If you only have one variable of each type then you only create one object of each type, which is all you need.
 
Think about this. Let's say that you and I are flatmates and we have a cupboard in which we keep are going to keep a container of cornflakes. You buy a new container, fill it with cornflakes and put it in the cupboard. I then buy a new container, fill it with cornflakes and put it in the cupboard, throwing yours aside when I do so. If I then empty the container that I bought, would you expect the container that you bought to magically empty as well? Of course not, so why are you expecting that same magic to happen in your app?

In your app, you are creating two DataSets. If you clear one, why would the other magically clear too? You're also creating two data adapters and two command builders. Is that really what you want? That said, you're filling a DataTable with the same name each time so, if you use one DataSet, you're going to end up with all the data in one DataTable. You should probably be using two DataTables. You can put them in the same DataSet or not use a DataSet at all. Also, if you're going to use two data adapters and two command builders then you should have two variables of each type as well. If you only have one variable of each type then you only create one object of each type, which is all you need.

Thanks Jim for the response. I finally did the right thing and got what I wanted. Once again, thanks and I'm very grateful.
 
Back
Top