Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Nuts N Bolts of ASP.net

3.67/5 (3 votes)
15 Feb 2011CPOL 8.4K  
I would like to share a brilliant piece of code as a continuation to my Nuts N Bolts which I encounter on my day to day developments and maintenance here and there:This is for removing repeated data from DataTale: public DataTable FilterDuplicateRows(DataTable dTable, string colName) ...

I would like to share a brilliant piece of code as a continuation to my Nuts N Bolts which I encounter on my day to day developments and maintenance here and there:


This is for removing repeated data from DataTale:


C#
 public DataTable FilterDuplicateRows(DataTable dTable, string colName) 
        {
            Hashtable hTable = new Hashtable();
            ArrayList duplicateList = new ArrayList();
            foreach (DataRow drow in dTable.Rows)
            {
                if (hTable.Contains(drow[colName]))
                    duplicateList.Add(drow);
                else
                    hTable.Add(drow[colName], string.Empty);
            }
            foreach (DataRow dRow in duplicateList)
                dTable.Rows.Remove(dRow);
            return dTable;
        }
Using the function:
private void BindGrid()
        {
            try
            {
                DataSet ds = (DataSet)ViewState["ds"];
                DataTable dt = (DataTable)ViewState["dt"];
                DataTable dtFiltered = new DataTable();               
                dtFiltered = FilterDuplicateRows(dt, "software");     
                gvRolePackage.DataSource = dtFiltered;                 
                gvRolePackage.DataBind();
             }
         }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)