Create DataTable from string

Padmanabhan, Venkatesh 120 Reputation points
2023-04-07T16:18:53.6866667+00:00

Hi. I am trying to create a data table from a string variable. The string contains information as below. string str = "where os_name in ('Region','Test-Daily','WWP','WWP3D','WWPF3D','Region','Test-BackDaily','P01D','WPI1D','WPF1D','Region','Test-CountryDaily','WIP02D','WPI2D','WPF2D','Region','Test-CountoDaily','DAIP04D','DAIPI4D','PF4D')"; I want a data table created from above string, which has 3 column names as - Region , Frequency , Name |Region|Frequency| Name| | -------- | -------- | -------- | | Region |Test-Daily| WWP | | Region |Test-Daily| WWP3D |

-like this and continuing till end How to achieve this in C#?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,010 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 46,456 Reputation points Microsoft Vendor
    2023-04-11T13:44:58.8666667+00:00

    Hi @Padmanabhan, Venkatesh ,Welcome to Q&A. You could try the following code to get what you wanted.

    Note that this code is only applicable when your strings are in the correct format and are all in groups of 5. The judgment of the error situation is not added.

    using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            public class region_case
            {
    
                public string Region { get; set; }
                public string Frequency { get; set; }
                public string Name { get; set; }
            }
            List<region_case> list = new List<region_case>();
            private void button1_Click(object sender, EventArgs e)
            {
                string str = "where os_name in ('Region','Test-Daily','WWP','WWP3D','WWPF3D','Region','Test-BackDaily','P01D','WPI1D','WPF1D','Region','Test-CountryDaily','WIP02D','WPI2D','WPF2D','Region','Test-CountoDaily','DAIP04D','DAIPI4D','PF4D')";
                string pattern = @"'(.*?)'"; //Regular expression pattern, match content inside single quotes
                MatchCollection matches = Regex.Matches(str, pattern); // Perform regular expression matching
    
                for (int i = 0; i < matches.Count; i++)
                {
                    string value = matches[i].Groups[1].Value;
    
                    if (value == "Region")
                    {
                        region_case region = new region_case();
                        region.Region = value;
                        region.Frequency = matches[i + 1].Groups[1].Value;
    
                        for (int j = 2; j < 5; j++)
                        {
                            region.Name = matches[i + j].Groups[1].Value;
                            list.Add(region);
                        }
                    }
                }
                dataGridView1.DataSource = list;
            }
        }
    }
    
    

    enter image description here

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.