Delete a row in one dataset that matches the row of another dataset

Posted on 2006-05-16
Last Modified: 2008-02-01
I need to delete a row in one dataset that matches the row of another dataset :

        Dim row As DataRow
        For Each row In DSTemp.Rows
          . . . .
          if condition . . .

                 >>  I want to remove a row in "DataSet1" that matches the current row in "DSTemp", above

                    DataSet1.Tables(0).Rows.Remove(  --  the row that matches DSTemp --  )  ???

Note: datasets  DSTemp and DataSet1 are identical
Is it possible to get the "index" or current position of DSTemp and set it to DataSet1?
I have the primary key field:   row("ProductID")          thanks
Question by:MikeMCSD
    LVL 6

    Expert Comment

    Hi MikeMCSD,

    I got the solution for ur problem

    u have to write the code for doing that operation the caode will be as follows,
                                                   DataSet ds = new DataSet();
                      ds = (DataSet)Session["Dataview"];

                      DataSet ds2  =new DataSet();
                      ds2 = (DataSet)Session["Data"];

                      for(int i=0; i<=ds.Tables[0].Rows.Count -1;i++)
                            DataRow re;
                            re = ds.Tables[0].Rows[i];
                            for(int j=0; j<=ds.Tables[0].Rows.Count -1;j++)
                                  DataRow re1;
                                  re1 = ds2.Tables[0].Rows[j];
                                  if (re[0].ToString() == re1[0].ToString())                                 {
                                              DataGrid2.DataSource = ds2;


    actauly i was calling this method on two dataset
    ds and ds1

    ds has 2 rows and ds1 has 4 rows out of witch 2 are iedentical to the ds rows(Both the database has onle one table and with same schema)

    and calles this code on button click
    ans used to session veriable to get the dataset back in the dataset object,

    for the folowing line of code

    u might have to change the value in place of 0 actually it represent the primary key column in the table.
    if (re[0].ToString() == re1[0].ToString())

    LVL 16

    Author Comment

    thanks Sandee . . but this is a Windows app. so I can't use Session, and
    I'm using VB . . so I'm sure how to convert a lot of it.
    LVL 6

    Expert Comment

    ok u can skip the session actuaaly i have implemented this code in Webapplication on Button click, and for retaining the dataset i am using the session.

    u can jst remove the session veriable and write the remaining code in the VB

    and if u want take this link for C# to VB convertion

    LVL 6

    Accepted Solution

    Or u can user this code
    --------------get ur two datasets-------------
    Dim ds As DataSet = New DataSet ------first
    Dim ds2 As DataSet = New DataSet --------second whose rows has to be deleted

    Dim i As Integer = 0
    While i <= ds.Tables(0).Rows.Count - 1
     Dim re As DataRow
     re = ds.Tables(0).Rows(i)
     Dim j As Integer = 0
     While j <= ds.Tables(0).Rows.Count - 1
       Dim re1 As DataRow
       re1 = ds2.Tables(0).Rows(j)
       If re(0).ToString = re1(0).ToString Then
         DataGrid2.DataSource = ds2
       End If
     End While
    End While


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now