DataTable通过索引将选定的行和列复制到另一个DataTable

时间:2016-10-03 09:38:06

标签: c# asp.net regex datatable

我有两个DataTable'

  • ImportExcelDT
  • ExportExcelDT

在这两个表中,其中一个表包含一些数据,我将每行检查为验证并从表中删除无效列并将其添加到第二个表中。

这是我使用的代码

            int Col = 0;
            using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + sheet1 + "]", excel_con))
            {
                oda.Fill(ImportExcelDT);

                if (ImportExcelDT.Rows.Count > 0)
                {
                    for (int i = 0; i < ImportExcelDT.Rows.Count; i++)
                    {

                        string EmpCod = ImportExcelDT.Rows[i]["Employee Code"].ToString();
                        bool isEmpCod = Regex.IsMatch(EmpCod, @"^[a-zA-Z0-9/-]+$");

                       //Here i am checking condition if its false
                       if (!isEmpCod)
                        {
                            count++;
                          //Now i want to copy and delete selected row in another data table 
                            for (int j = 0; j < ImportExcelDT.Rows.Count; j++)
                            {
                                ImportExcelDT.Rows[j][i] = ExportExcelDT.Rows[j][Col];
                                ImportExcelDT.Rows[i].Delete();

                            }
                        }
                        else
                        {

                        }

我对哪一排&amp;我应该在ImportExcelDT.Rows[j][i]ExportExcelDT.Rows[j][Col];上选择列,同时删除并添加它。

1 个答案:

答案 0 :(得分:1)

尝试这个

if (ImportExcelDT.Rows.Count > 0)
{
    ExportExcelDT = ImportExcelDT.Clone();
    for (int i = 0; i < ImportExcelDT.Rows.Count; i++)
    {
        string EmpCod = ImportExcelDT.Rows[i]["Employee Code"].ToString();
        bool isEmpCod = Regex.IsMatch(EmpCod, @"^[a-zA-Z0-9/-]+$");
        //Here i am checking condition if its false
            if (!isEmpCod)
            {
                count++;
                //Now i want to copy and delete selected row in another data table 
                ExportExcelDT.ImportRow(ImportExcelDT.Rows[i]);
                ImportExcelDT.Rows[i].Delete();
            }
        else
        {

        }
    }
}
相关问题