Blog

Filter one DataTable and store it another DataTable

Posted by Rohit Sharma Blogs | ASP.NET Programming Dec 02, 2012
some time a condition come where a value store in DataTable we want to filter this DataTable also.
Dear friend 
Here you can see that a First data is store in DataTable (dt) but i want to filter that DataTable where value "SAM" is not present.

so you follow this way.


int i=0;
          DataTable dtt=new DataTable();
            dtt.Columns.Add("SSN", typeof(string));
            dtt.Columns.Add("NAME", typeof(string));
            dtt.Columns.Add("ADDR", typeof(string));
            dtt.Columns.Add("AGE", typeof(int));
            dtt.Rows.Add();

            DataTable dt = new DataTable();
            dt.Columns.Add("SSN", typeof(string));
            dt.Columns.Add("NAME", typeof(string));
            dt.Columns.Add("ADDR", typeof(string));
            dt.Columns.Add("AGE", typeof(int));
            // Showing how to set Primary Key(s) in a Data table (Although it's not compulsory to have one)
            DataColumn[] keys = new DataColumn[1];
            keys[0] = dt.Columns[0];
            dt.PrimaryKey = keys;
            dt.Rows.Add("203456876", "John", "12 Main Street, Newyork, NY", 15);
            dt.Rows.Add("203456877", "SAM", "13 Main Ct, Newyork, NY", 25);
            dt.Rows.Add("203456878", "Elan", "14 Main Street, Newyork, NY", 35);
            dt.Rows.Add("203456879", "Smith", "12 Main Street, Newyork, NY", 45);
            dt.Rows.Add("203456880", "SAM", "345 Main Ave, Dayton, OH", 55);
            dt.Rows.Add("203456881", "Sue", "32 Cranbrook Rd, Newyork, NY", 65);
            dt.Rows.Add("203456882", "Winston", "1208 Alex St, Newyork, NY", 65);
            dt.Rows.Add("203456883", "Mac", "126 Province Ave, Baltimore, NY", 85);
            dt.Rows.Add("203456884", "SAM", "126 Province Ave, Baltimore, NY", 95);
          //  DataRow[] DataTable.Select(string filterExpression, string sort)
            foreach (DataRow o in dt.Select("NAME <> 'SAM'"))
            {
               
              //  Console.WriteLine("\t" + o["SSN"] + "\t" + o["NAME"] + "\t" + o["ADDR"] + "\t" + o["AGE"]);
                string aa = o["SSN"].ToString();
                string ab = o["NAME"].ToString();
                string ac = o["ADDR"].ToString();
                string ad = o["AGE"].ToString();

                //for (int i = 0; i < dt.Rows.Count; i++)
                //{
                    dtt.Rows[i][0] = aa;
                    dtt.Rows[i][1] = ab;
                    dtt.Rows[i][2] = ac;
                    dtt.Rows[i][3] = ad;
                    dtt.Rows.Add();
                //}
                //dtt.ImportRow(dt.Rows[0]);
                    i = i + 1;
            }
          
        

post comment
     
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter