日期:2014-05-18 浏览次数:20905 次
string[] distinctcols = new string[(DTA.Columns.Count)]; foreach (DataColumn dc in DTA.Columns) { distinctcols[dc.Ordinal] = dc.ColumnName; } DataView mydataview = new DataView(DTA); DataTable DTB= mydataview.ToTable(true, distinctcols);
------解决方案--------------------
public static bool CheckReduplicated(DataTable dt, ref string strMsg, params string[] keyFields) { string f = ""; for (int i = 0; i < keyFields.Length; i++) { if (i != keyFields.Length - 1) f += keyFields[i] + ","; else f += keyFields[i] + " ASC"; } dt.DefaultView.Sort = f; DataTable temp = dt.DefaultView.ToTable(); for (int i = 0; i < temp.Rows.Count; i++) { DataRow dr = temp.Rows[i]; if (i > 0) { DataRow dr_pre = temp.Rows[i - 1]; bool isReduplicated = true; foreach (string s in keyFields) { string value = dr[s].ToString(); string value_pre = dr_pre[s].ToString(); if (value != value_pre) { isReduplicated = false; } } if (isReduplicated) { if (strMsg.Length > 0) { strMsg += " "; } foreach (string s in keyFields) { strMsg += string.Format(@"[{0}:{1}]", s, dr[s].ToString()); } strMsg += "有重复."; return true; } } } return false; }