zoukankan      html  css  js  c++  java
  • 删除datatable的重复行

    删除datatable的重复行

    private void RemoveRepeat(ref System.Data.DataTable dataTableSource)
            {
                List<string> list = new List<string>();
                StringBuilder builder = null;
                System.Data.DataRow currenDr = null;
                string currentStr = string.Empty;
                for (int i = dataTableSource.Rows.Count-1; i >= 0;i-- )
                {
                    if (dataTableSource.Rows[i]==null)
                    {
                        continue;
                    }
                    currenDr=dataTableSource.Rows[i];
                    builder = new StringBuilder();
                    for (int j = 0; j < dataTableSource.Columns.Count;j++ )
                    {
                        builder.AppendFormat("{0}={1}", 
                                dataTableSource.Columns[j].ColumnName,
                                currenDr[dataTableSource.Columns[j].ColumnName] == DBNull.Value ?
                                    "NULL" : 
                                    currenDr[dataTableSource.Columns[j].ColumnName].ToString()
                            );
                        if (j < dataTableSource.Columns.Count-1)
                        {
                            builder.Append(",");
                        }
                    }
                    currentStr = builder.ToString().Trim();
     
                    if (!list.Contains(currentStr))
                    {
                        list.Add(currentStr);
                    }
                    else
                    {
                        dataTableSource.Rows.RemoveAt(i);
                        dataTableSource.AcceptChanges();
                    }
                }
            }
  • 相关阅读:
    NYOJ 10 skiing DFS+DP
    51nod 1270 数组的最大代价
    HDU 4635 Strongly connected
    HDU 4612 Warm up
    POJ 3177 Redundant Paths
    HDU 1629 迷宫城堡
    uva 796
    uva 315
    POJ 3180 The Cow Prom
    POJ 1236 Network of Schools
  • 原文地址:https://www.cnblogs.com/hongjiumu/p/3043812.html
Copyright © 2011-2022 走看看