[Webinar] Streamline your web hosting managementRegister Today


C# DataTable

Posted on 2010-03-31
Medium Priority
Last Modified: 2013-12-17

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

Question by:Planmaster

Expert Comment

ID: 29184245
Use DataTable.Select()
LVL 10

Accepted Solution

Mathiyazhagan earned 500 total points
ID: 29186339
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


Author Closing Comment

ID: 31709328
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Suggested Courses

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question