C# DataTable


I am trying to create an application in C# and VS2005, but I've stumbled into a problem with DataTables and I was hoping someone could help.

I have a DataTable, populated using ADO and the SqlDataAdapter that looks similar to the picture below (Its a simplified datatable screenshot from SQL Server Management Studio for illistration purposes). As you can see, there is more that 1 record that shares the same test_case value but is made unique by the Test_type column.
My query returns the data similar to the picture below, it is ordered by test_case and then test_type.

The steps I'd like to take...

1) Create a subset from the datatable of all records sharing the same test_case value (e.g All records where Test_Case = 2) ,preserving the order in the image.

2) Do some processing with each row of the subset

3) Loop and repeat the process for the next subset of records (Where test_case = 3) and so on.

The image only shows two of each test_case value, but more advanced versions may have 10 + records with the same test_case.

Thanks in advance

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Use DataTable.Select()
This can be done by following procedure:
(1) Find unique records regarding with test_Case column in datatable.
     DataTable dtDistinct = dt.DefaultView.ToTable(true, "test_case");
  here , we are creating a datatable with test_case calumns with Unique value (true for distinct)
(2)Select all the records matching for each unique value and process selected records.

have attcahed sample code. hope this helps.
DataTable dt = new DataTable();
            dt.Columns.Add("test_case", typeof(int));
            dt.Columns.Add("validation_time", typeof(string ));
            dt.Columns.Add("test_type", typeof(int));
            dt.Rows.Add(2, "10.00", 1);
            dt.Rows.Add(2, "10.03", 2);
            dt.Rows.Add(3, "10.00", 1);
            dt.Rows.Add(3, "10.03", 2);
            dt.Rows.Add(4, "10.00", 1);
            dt.Rows.Add(4, "10.03", 2);
            dt.Rows.Add(5, "10.00", 1);
            dt.Rows.Add(5, "10.03", 2);
            dt.Rows.Add(6, "10.00", 1);
            dt.Rows.Add(6, "10.03", 2);
            dt.Rows.Add(7, "10.00", 1);
            dt.Rows.Add(7, "10.03", 2);

             //create a datatable with test_case with unique values
            DataTable dtDistinct = dt.DefaultView.ToTable(true, "test_case");

            foreach (DataRow rowDistinct in dtDistinct.Rows)
                Console.WriteLine("Test Case # "+rowDistinct["test_case"]);  
                DataRow[] rows = dt.Select("test_case=" + rowDistinct["test_case"]);
                foreach (DataRow rowProcess in rows)
                    //Process rows
                    Console.WriteLine("validation_time :{0}, test_type={1}", rowProcess["validation_time"], rowProcess["test_type"]);    

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PlanmasterAuthor Commented:
Mathiyazhagan, that was genius. I'd worked out the second part by the time I read the solution but getting the unique test case values was inspired. Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.