Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Nuts N Bolts of ASP.net

Rate me:
Please Sign up or sign in to vote.
3.67/5 (3 votes)
15 Feb 2011CPOL 7.9K   1   2
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)


Written By
Software Developer
India India
The Author holds degrees in Bachelor as well as Master of Computer Application. He firmly believes in the philosophy 'Sound Mind in Sound Body'. He loves Jogging, exersising and you may find him lost in deep thoughts when free.He feels Necessity is the mother of all logics and Programming can't be work but challenge whose output is fun n happiness.Bathroom is the place where he finds heaven. For a cloud application demo please do visit
https://www.youtube.com/watch?v=klGX6U0uURI

Comments and Discussions

 
GeneralReason for my vote of 1 Brilliant? Why not filtering with a... Pin
nmeteora22-Feb-11 3:41
nmeteora22-Feb-11 3:41 
Reason for my vote of 1
Brilliant?
Why not filtering with a View instead of deleting data from the table?
GeneralReason for my vote of 5 This is a great way to eliminate rep... Pin
Anantharaman_N21-Feb-11 22:32
Anantharaman_N21-Feb-11 22:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.