/// <summary> /// 合并DataTable,经保证两个DataTable的结构相同哦 /// </summary> /// <param name="un_duplicated">不重复的列</param> public DataTable SumDataTable(DataTable dt1, DataTable dt2, string un_duplicated) { for (int i = 0; i < dt2.Rows.Count; i++) { DataRow dr = dt1.NewRow(); for (int j = 0; j < dt2.Columns.Count; j++) { dr[j] = dt2.Rows[i][j]; } //判断是否重复 bool IsDuplicated = false; for (int k = 0; k < dt1.Rows.Count; k++) { if (dt1.Rows[k][un_duplicated].ToString() == dr[un_duplicated].ToString()) IsDuplicated = true; } //没有时重复才合并 if (!IsDuplicated) dt1.Rows.Add(dr); } return dt1; }
//调用
if ((dt == null)) dt = dt2;
else dt = SumDataTable(dt, dt2, "name");
/// <summary> /// 筛选DataTable,返回筛选出哪列中,含有该值的DataTable /// </summary> /// <param name="dt"></param> /// <param name="Column">指定第几列</param> /// <param name="ColumnValue">该列的值</param> /// <returns></returns> public DataTable SelectDataTable(DataTable dt, int Column, string ColumnValue) { try { DataTable dtSelectTable = dt.Clone(); dtSelectTable.Clear(); for (int i = 0; i < dt.Rows.Count; i++) { if (dt.Rows[i][Column].ToString() == ColumnValue) { DataRow dr = dtSelectTable.NewRow(); for (int j = 0; j < dt.Columns.Count; j++) { dr[j] = dt.Rows[i][j]; } dtSelectTable.Rows.Add(dr); } } return dtSelectTable; } catch (Exception) { return null; } }