none
How to create a new datatable from old datatable and select rows from old datatble? RRS feed

  • Question

  • hi all

    i have a data table in my dataset1

    i fill datatable by this codes:

      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'DataSet1.Person' table. You can move, or remove it, as needed.
            Me.PersonTableAdapter.Fill(Me.DataSet1.Person)
         
        End Sub

    now i want Create a new Datatable by some of fields(PersonId,Name,Family)

     and get rows from Person DataTable.

    how to solve this problem

    please help me

    thanks all



    Name of Allah, Most Gracious, Most Merciful and He created the human


    • Edited by sh 2020 Saturday, December 13, 2014 9:03 AM
    Saturday, December 13, 2014 9:01 AM

Answers

  • Please try to use the following code

     DataTable dataTable = new DataTable();
                dataTable.Columns.Add(new DataColumn("PersonId",typeof(int)));
                dataTable.Columns.Add(new DataColumn("Name",typeof(string)));
                dataTable.Columns.Add(new DataColumn("Family",typeof(string)));
    
                foreach (DataRow row in this.DataSet1.Person.Rows)
                {
                    DataRow dataRow = dataTable.NewRow();
                    dataRow["PersonId"] = row["PersonId"];
                    dataRow["Name"] = row["Name"];
                    dataRow["Family"] = row["Family"];
    
                    dataTable.Rows.Add(dataRow);
                }                   

    Sunday, December 14, 2014 6:49 AM
  •    Dim CopyOfDataTable As New DataTable
                    CopyOfDataTable = Frm1.Dataset1.Person.CopyToDataTable

    Name of Allah, Most Gracious, Most Merciful and He created the human

    Sunday, December 21, 2014 5:03 PM

All replies

  • Please try to use the following code

     DataTable dataTable = new DataTable();
                dataTable.Columns.Add(new DataColumn("PersonId",typeof(int)));
                dataTable.Columns.Add(new DataColumn("Name",typeof(string)));
                dataTable.Columns.Add(new DataColumn("Family",typeof(string)));
    
                foreach (DataRow row in this.DataSet1.Person.Rows)
                {
                    DataRow dataRow = dataTable.NewRow();
                    dataRow["PersonId"] = row["PersonId"];
                    dataRow["Name"] = row["Name"];
                    dataRow["Family"] = row["Family"];
    
                    dataTable.Rows.Add(dataRow);
                }                   

    Sunday, December 14, 2014 6:49 AM
  •    Dim CopyOfDataTable As New DataTable
                    CopyOfDataTable = Frm1.Dataset1.Person.CopyToDataTable

    Name of Allah, Most Gracious, Most Merciful and He created the human

    Sunday, December 21, 2014 5:03 PM