简体   繁体   中英

How to copy specific rows from one datatable to another based on a specific datarow

在此处输入图片说明

in the students table I want only students users from the users table to be copied , and also in the faculty table I want only faculty users to be copied from the users table. how can I format a new loop for this issue. thank you.

The following is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Data.OleDb;
using System.Web.UI.WebControls;

public partial class BulkUser : System.Web.UI.Page
{
    DataTable Gendt = new DataTable();
    DataTable Usersdt = new DataTable();
    DataTable Studentsdt = new DataTable();
    DataTable Facultydt = new DataTable();


    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void UsersGridView_DataBinding(object sender, EventArgs e)
    {
        UsersGridView.DataSource = Gendt;
    }


    protected void StudentsGridView_DataBinding(object sender, EventArgs e)
    {
        StudentsGridView.DataSource = Studentsdt;
    }

    protected void FacultyGridView_DataBinding(object sender, EventArgs e)
    {
        FacultyGridView.DataSource = Facultydt;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        OleDbConnection cnn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(@"~\data\University.xlsx") + "; Extended Properties=Excel 12.0;");
        OleDbCommand oconn = new OleDbCommand("select * from [Sheet1$]", cnn);
        cnn.Open();
        OleDbDataAdapter adp = new OleDbDataAdapter(oconn);
        adp.Fill(Gendt);
        cnn.Close();


        Usersdt = Gendt.Copy();
        Studentsdt = Gendt.Copy();
        Studentsdt.Columns.Remove("UserType");

        Facultydt = Studentsdt.Copy();

        //foreach (DataRow dr in Usersdt.Rows)
        //{
        //    foreach (DataRow dr1 in Studentsdt.Rows)
        //    {

        //        ////if (dr["UserType"].ToString().Contains("student"))
        //        //    Studentsdt.ImportRow(Student); 
        //    }

        UsersGridView.DataBind();
        StudentsGridView.DataBind();
        FacultyGridView.DataBind();
    }
}

You can follow this logic to search the UsersTable and copy whole row (except last column) to the destination tables based on a column value. This method has a better performance as it traverses the UsersTable once.

foreach(DataRow dRow in Usersdt.Rows)
{
    DataTable destinationTable = null;

    if (dRow.Field<string>("UserType") == "student")
        destinationTable = Studentsdt;
    else if (dRow.Field<string>("UserType") == "Faculty")
        destinationTable = Facultydt;
    else
        continue; // skip the current row

    // copy the row skipping the last column as required
    object[] rowData = new object[dRow.ItemArray.Length - 1];
    Array.ConstrainedCopy(dRow.ItemArray, 0, rowData, 0, rowData.Length);
    destinationTable.Rows.Add(rowData);
}

suppose your data is in Usersdt then you need to filter them as

DataRow[] studentRows= Usersdt.Select("UserType=student");
DataRow[] facultyRows= Usersdt.Select("UserType=Faculty");

// import filtered rows in student and faculty tables one by one
foreach (DataRow row in studentRows) 
{
   Studentdt.ImportRow(row);
}
foreach (DataRow row in facultyRows) 
{
   Facultydt.ImportRow(row);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM