當前位置: 首頁>>代碼示例>>C#>>正文


C# DataTable.Clone方法代碼示例

本文整理匯總了C#中System.Data.DataTable.Clone方法的典型用法代碼示例。如果您正苦於以下問題:C# DataTable.Clone方法的具體用法?C# DataTable.Clone怎麽用?C# DataTable.Clone使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Data.DataTable的用法示例。


在下文中一共展示了DataTable.Clone方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: RemoveDuplicates

        public static DataTable RemoveDuplicates(DataTable dt, DataColumn[] keyColumns, string SavePath, string currentMapping)
        {
            int rowNdx = 0;
            var res = new List<DataTable>();
            DataTable duplicatesTable = dt.Clone();

            var qFields = string.Join(", ", keyColumns.Select(x => "it[\"" + x.ColumnName + "\"] as " + x.ColumnName.Replace(" ","") + ""));

            var q = dt.AsEnumerable()
                                .AsQueryable()
                                .GroupBy("new(" + qFields + ")", "it")
                                .Select("new (it as Data)");

            var dtemp = dt.Clone();

            foreach (dynamic d in q)
            {
               // dtemp.Rows.Add(d.Data.First().ItemArray());
                rowNdx = 0;
                foreach (var row in d.Data)
                {
                    if (rowNdx == 0)
                    {
                        dtemp.Rows.Add(row.ItemArray);
                    }
                    else
                    {
                        duplicatesTable.Rows.Add(row.ItemArray);
                    }
                    rowNdx++;
                }
            }

            string newFileName = string.Format("{0}{1}", "test", rowNdx);
            FileSaveHelper.ExportDataTable(duplicatesTable, string.Format("{0}\\{1}", SavePath, currentMapping + "_Duplicates.xlsx"));
            return dtemp;
            //while (rowNdx < tbl.Rows.Count - 1)
            //{
            //    DataRow[] dups = FindDups(tbl, rowNdx, keyColumns);

            //    if (dups.Length > 0)
            //    {
            //        foreach (DataRow dup in dups)
            //        {
            //            duplicatesTable.ImportRow(dup);
            //            tbl.Rows.Remove(dup);
            //        }
            //        tbl.Rows.Add(dups.FirstOrDefault());
            //    }
            //    else
            //    {
            //        rowNdx++;
            //    }
            //}
               // return duplicatesTable;
        }
開發者ID:kavamara,項目名稱:CAMS_ETL,代碼行數:56,代碼來源:DataSetHelper.cs

示例2: CompareDt

        /// <summary>
        /// �Ƚ�����DataTable���ݣ��ṹ��ͬ��
        /// </summary>
        /// <param name="dt1">�������ݿ��DataTable</param>
        /// <param name="dt2">�����ļ���DataTable</param>
        /// <param name="keyField">�ؼ��ֶ���</param>
        /// <param name="dtRetAdd">�������ݣ�dt2�е����ݣ�</param>
        /// <param name="dtRetDif1">��ͬ�����ݣ����ݿ��е����ݣ�</param>
        /// <param name="dtRetDif2">��ͬ�����ݣ�ͼ2�е����ݣ�</param>
        /// <param name="dtRetDel">ɾ�������ݣ�dt2�е����ݣ�</param>
        public void CompareDt(DataTable dt1, DataTable dt2, string keyField,
            out DataTable dtRetAdd, out DataTable dtRetDif1, out DataTable dtRetDif2,
            out DataTable dtRetDel)
        {
            //Ϊ�����������ṹ
            dtRetDel = dt1.Clone();
            dtRetAdd = dtRetDel.Clone();
            dtRetDif1 = dtRetDel.Clone();
            dtRetDif2 = dtRetDel.Clone();

            int colCount = dt1.Columns.Count;

            DataView dv1 = dt1.DefaultView;
            DataView dv2 = dt2.DefaultView;

            //���Ե�һ����Ϊ���գ����ڶ��������޸��˻���ɾ����
            foreach (DataRowView dr1 in dv1)
            {
                dv2.RowFilter = keyField + " = '" + dr1[keyField].ToString() + "'";
                if (dv2.Count > 0)
                {
                    if (!CompareUpdate(dr1, dv2[0]))//�Ƚ��Ƿ��в�ͬ��
                    {
                        dtRetDif1.Rows.Add(dr1.Row.ItemArray);//�޸�ǰ
                        dtRetDif2.Rows.Add(dv2[0].Row.ItemArray);//�޸ĺ�
                       // dtRetDif2.Rows[dtRetDif2.Rows.Count - 1]["FID"] = dr1.Row["FID"];//��ID���������ļ��ı����Ϊ����IDȫ��==0
                        continue;
                    }
                }
                else
                {
                    //�Ѿ���ɾ����
                    dtRetDel.Rows.Add(dr1.Row.ItemArray);
                }
            }

            //�Ե�һ����Ϊ���գ�����¼�Ƿ���������
            dv2.RowFilter = "";//�������
            foreach (DataRowView dr2 in dv2)
            {
                dv1.RowFilter = keyField + " = '" + dr2[keyField].ToString() + "'";
                if (dv1.Count == 0)
                {
                    //������
                    dtRetAdd.Rows.Add(dr2.Row.ItemArray);
                }
            }
        }
開發者ID:pcstx,項目名稱:OA,代碼行數:58,代碼來源:201010.aspx.cs

示例3: BindReferrers

    private void BindReferrers(string day)
    {
        string filename = Server.MapPath(BlogSettings.Instance.StorageLocation + "Log/" + day + ".xml");
        if (File.Exists(filename))
        {
          DataSet ds = new DataSet();
          ds.ReadXml(filename);

          DataTable table = new DataTable();
          table.Columns.Add("url", typeof(string));
          table.Columns.Add("shortUrl", typeof(string));
          table.Columns.Add("hits", typeof(int));

          DataTable spamTable = table.Clone();

          foreach (DataRow row in ds.Tables[0].Rows)
          {
        DataRow newRow = table.NewRow();
        if (row.Table.Columns.Contains("isSpam") && row["isSpam"].ToString().ToLowerInvariant() == "true")
          newRow = spamTable.NewRow();

        newRow["url"] = Server.HtmlEncode(row["address"].ToString());
        newRow["shortUrl"] = MakeShortUrl(row["address"].ToString());
        newRow["hits"] = int.Parse(row["url_text"].ToString());

        if (row.Table.Columns.Contains("isSpam") && row["isSpam"].ToString().ToLowerInvariant() == "true")
          spamTable.Rows.Add(newRow);
        else
          table.Rows.Add(newRow);
          }

          BindTable(table, grid);
          BindTable(spamTable, spamGrid);
        }
    }
開發者ID:chandru9279,項目名稱:StarBase,代碼行數:35,代碼來源:Referrers.aspx.cs

示例4: Reverse

        /// <summary>
        ///     DataTable倒序
        /// </summary>
        /// <param name="dt">源DataTable</param>
        public static DataTable Reverse(DataTable dt)
        {
            var tmpDt = dt.Clone();

            for (var i = dt.Rows.Count - 1; i >= 0; i--) { tmpDt.ImportRow(dt.Rows[i]); }
            return tmpDt;
        }
開發者ID:FarseerNet,項目名稱:Farseer.Net.Utils,代碼行數:11,代碼來源:DataTableExtend.cs

示例5: SortLogicalTable

        public static DataTable SortLogicalTable(DataTable TableSort, string FieldSort)
        {
            if(TableSort==null)return null;
            DataTable dtBK = TableSort.Clone();
            try
            {
                DataRow[] rows = new DataRow[TableSort.Rows.Count];
                TableSort.Rows.CopyTo(rows, 0);
                Array.Sort(rows, new DataRowLogicalComparer(FieldSort));
                foreach (DataRow r in rows)
                {
                    dtBK.ImportRow(r);
                }
                if (TableSort.DataSet != null)
                {
                    DataSet ds = new DataSet();
                    ds.Tables.Add(dtBK);
                }
                return dtBK;
            }
            catch
            {

            }
            return TableSort;
        }
開發者ID:khanhdtn,項目名稱:did-vlib-2011,代碼行數:26,代碼來源:AppUtil.cs

示例6: Get_Tarifas

        private void Get_Tarifas()
        {
            string query = frete.Properties.Resources.sql_Select_TarifasPedido;

            List<string> campos = new List<string>();

            campos.Add("idtarifas");
            campos.Add("transportadora");
            campos.Add("cidade");

            DBConnect dbc = new DBConnect();

            dtDisponiveis = dbc.Select(query, campos.Count, campos);

            dtDisponiveis.Columns.Add("copiado", typeof(bool));

            foreach (DataRow dr in dtDisponiveis.Rows)
                dr["copiado"] = false;

            dgv_Disponiveis.DataSource = dtDisponiveis;
            dtSelecionados = dtDisponiveis.Clone();
            dtSelecionados.Columns.Add("index_disp", typeof(int));

            dgv_Selecionados.DataSource = dtSelecionados;

        }
開發者ID:PhillBeck,項目名稱:frete,代碼行數:26,代碼來源:frm_Seleciona.cs

示例7: SortDataTable

        /// <summary>
        /// Sorts the <see cref="System.Data.DataTable"/>.
        /// </summary>
        /// <param name="sourceTable">The source table.</param>
        /// <param name="sortColumn">The column to sort by.</param>
        /// <param name="order">The <see cref="Tools.Common.SortOrder"/>.</param>
        /// <param name="copyIfNoSort">Should only be true when one table owner is assumed
        /// other time and space.</param>
        /// <returns></returns>
        public static DataTable SortDataTable(DataTable sourceTable, string sortColumn, SortOrder order, bool copyIfNoSort)
        {
            if (String.IsNullOrEmpty(sortColumn))
            {
                // copyIfNoSort make the semantic more stable, even if there is no sort required
                // the copy is returned, so the caller may assume same level of independence.
                if (copyIfNoSort) return sourceTable.Copy();
                return sourceTable;
            }

            DataTable targetTable = sourceTable.Clone();

            DataRow[] sourceRows = sourceTable.Select(null, 
                ("[" + sortColumn + "] " + order.ToString()).TrimEnd(' '));

            for (int i = 0; i < sourceRows.Length; i++)
            {
                DataRow newRow = targetTable.NewRow();

                newRow.ItemArray = sourceRows[i].ItemArray;

                targetTable.Rows.Add(newRow);
            }
            return targetTable;
        }
開發者ID:alienwaredream,項目名稱:toolsdotnet,代碼行數:34,代碼來源:SortUtility.cs

示例8: ConvertTableType

		public static DataTable ConvertTableType(DataTable dt, Dictionary<string, Type> types)
		{
			DataTable newDt = dt.Clone();
			//convert all columns' datatype

			newDt.TableName = dt.TableName;

			foreach (DataColumn dc in newDt.Columns)
			{
				if (types.ContainsKey(dc.ColumnName))
				{
					dc.DataType = types[dc.ColumnName];
				}
			}

			//import data from original table
			//foreach (DataRow dr in dt.Rows)
			//{
			//	newDt.ImportRow(dr);
			//}

			FillData(dt, newDt);

			dt.Dispose();
			return newDt;
		}
開發者ID:justdude,項目名稱:DbExport,代碼行數:26,代碼來源:CDataTableHelper.cs

示例9: GetPagedTable

        public static DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)
        {
            if (PageIndex == 0)
                return dt;
            DataTable newdt = dt.Clone();
            //newdt.Clear();
            int rowbegin = (PageIndex - 1) * PageSize;
            int rowend = PageIndex * PageSize;

            if (rowbegin >= dt.Rows.Count)
                return newdt;

            if (rowend > dt.Rows.Count)
                rowend = dt.Rows.Count;
            for (int i = rowbegin; i <= rowend - 1; i++)
            {
                DataRow newdr = newdt.NewRow();
                DataRow dr = dt.Rows[i];
                foreach (DataColumn column in dt.Columns)
                {
                    newdr[column.ColumnName] = dr[column.ColumnName];
                }
                newdt.Rows.Add(newdr);
            }
            return newdt;
        }
開發者ID:youthjoy,項目名稱:qxhelper,代碼行數:26,代碼來源:Sys_Config_ListPageController.cs

示例10: carregarDttUserCampo

 private void carregarDttUserCampo()
 {
     dtUserCampo = user.getCamposUsuario();
     dtAtual = dtUserCampo.Clone();
     dtAtual.Clear();
     dtgvCampos.DataSource = dtAtual;
 }
開發者ID:4nub1s,項目名稱:infoGym,代碼行數:7,代碼來源:frmUserPerm.cs

示例11: CloneTable

        public static DataTable CloneTable(DataTable originalTable)
        {
            DataTable newTable;
            newTable = originalTable.Clone();

            return newTable;
        }
開發者ID:pyramidsolutions,項目名稱:VMSWebSln,代碼行數:7,代碼來源:DBUtility.cs

示例12: GridViewDataBind

    /// <summary>
    /// 綁定數據到GridView,當表格數據為空時顯示表頭
    /// </summary>
    /// <param name="gridview"></param>
    /// <param name="table">not null</param>
    public static void GridViewDataBind(GridView gridview, DataTable table)
    {
        if (table == null)
            throw new NullReferenceException();
        //記錄為空重新構造Gridview
        if (table.Rows.Count == 0)
        {
            table = table.Clone();
            table.Rows.Add(table.NewRow());
            gridview.DataSource = table;
            gridview.DataBind();
            int columnCount = table.Columns.Count;
            gridview.Rows[0].Cells.Clear();
            gridview.Rows[0].Cells.Add(new TableCell());
            gridview.Rows[0].Cells[0].ColumnSpan = columnCount;
            gridview.Rows[0].Cells[0].Text = EmptyText;
            gridview.Rows[0].Cells[0].Style.Add("text-align", "center");
        }
        else
        {
            //數據不為空直接綁定
            gridview.DataSource = table;
            gridview.DataBind();
        }

        //重新綁定取消選擇
        gridview.SelectedIndex = -1;
    }
開發者ID:0jpq0,項目名稱:Scut,代碼行數:33,代碼來源:GridViewNull.cs

示例13: SortDataTable

        public static DataTable SortDataTable(DataTable dt, string sort)
        {
            DataTable newDT = dt.Clone();
            int rowCount = dt.Rows.Count;
            DataRow[] foundRows = dt.Select(null, sort); // Sort with Column name
            for (int i = 0; i < rowCount; i++)
            {
                object[] arr = new object[dt.Columns.Count];
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    arr[j] = foundRows[i][j];
                }
                DataRow data_row = newDT.NewRow();
                data_row.ItemArray = arr;
                newDT.Rows.Add(data_row);
            }
            //clear the incoming dt
            dt.Rows.Clear();
            for (int i = 0; i < newDT.Rows.Count; i++)
            {
                object[] arr = new object[dt.Columns.Count];
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    arr[j] = newDT.Rows[i][j];
                }
                DataRow data_row = dt.NewRow();
                data_row.ItemArray = arr;
                dt.Rows.Add(data_row);
            }

            return dt;
        }
開發者ID:advanced-joelloyd,項目名稱:UghWebforms,代碼行數:32,代碼來源:Functions.cs

示例14: SetPage

        public static DataTable SetPage(DataTable dt, int currentPageIndex, int pageSize)
        {
            if (currentPageIndex == 0)
            {
                return dt;
            }
            DataTable newdt = dt.Clone();
            int rowbegin = (currentPageIndex - 1) * pageSize;   //當前頁的第一條數據在dt中的地位
            int rowend = currentPageIndex * pageSize;           //當前頁的最後一條數據在dt中的地位

            if (rowbegin >= dt.Rows.Count)
            {
                return newdt;
            }
            if (rowend > dt.Rows.Count)
            {
                rowend = dt.Rows.Count;
            }
            DataView dv = dt.DefaultView;
            for (int i = rowbegin; i <= rowend - 1; i++)
            {
                newdt.ImportRow(dv[i].Row);
            }
            return newdt;
        }
開發者ID:uwitec,項目名稱:wms_rfid,代碼行數:25,代碼來源:ExportExcelHelper.cs

示例15: GridViewDataBind

    ///<summary>
    ///綁定數據到GridView,當表格數據為空時顯示表頭
    ///</summary>
    ///<param name="gridview"></param>
    ///<param name="table"></param>
    public static void GridViewDataBind(GridView gridview, DataTable table)
    {
        //記錄為空重新構造Gridview
        EmptyText = Resources.MyGlobal.NoDataText;
        if (table.Rows.Count == 0)
        {
            table = table.Clone();
            table.Rows.Add(table.NewRow());

            int columnCount = table.Columns.Count;
            gridview.DataSource = table;
            gridview.DataBind();
            gridview.Rows[0].Cells.Clear();
            gridview.Rows[0].Cells.Add(new TableCell());
            gridview.Rows[0].Cells[0].ColumnSpan = columnCount;
            gridview.Rows[0].Cells[0].Text = EmptyText;
            gridview.Rows[0].Cells[0].Style.Add("text-align", "center");
            gridview.Rows[0].Cells[0].Style.Add("color", "red");
            gridview.Rows[0].Style.Add("height", "24px");
            //gridview.HeaderRow.Enabled = false;
        }
        else
        {
            //數據不為空直接綁定
            gridview.DataSource = table;
            gridview.DataBind();
        }

        //重新綁定取消選擇
        gridview.SelectedIndex = -1;
    }
開發者ID:WuziyiaoKingIris20140501,項目名稱:KFC,代碼行數:36,代碼來源:GridviewControl.cs


注:本文中的System.Data.DataTable.Clone方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。