Import data from a text file into a datagrid view

AMER SAID 396 Reputation points
2023-01-02T13:15:56.787+00:00

hi

I have a text file with a number of rows in the following format for each row.

"id":"CumtJt6BpbJbTcDFhrE0Lw_0000","full_name":"hasmukh "  

Where the first syllable is the name of the column and the second is the cell inside each column.
This is a simple sample file with more than 300 columns and thousands of rows. This is a sample for clarification

I would like to display the data in a DataTable or DataGrid View, since the data is undefined and so are the rows

id full_name
CumtJt6BpbJbTcDFhrE0Lw_0000" hasmukh

Developer technologies C#
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2023-01-03T02:10:29.447+00:00

    @AMER SAID , Welcome to Microsoft Q&A, you could use string.split and linq method to import data from a text file into a datagridview.

    private void button1_Click(object sender, EventArgs e)  
    {  
        var lines= File.ReadAllLines("test.txt");  
    
        var list = (from m in lines.ToList()  
                   select new Example  
                   {  
                       id = m.Split(new string[] { ",", ":" }, StringSplitOptions.None)[1].Replace("\"", ""),  
                       full_name = m.Split(new string[] { ",", ":" }, StringSplitOptions.None)[3].Replace("\"", ""),  
                   }).ToList();  
        dataGridView1.DataSource = list;  
          
    
    }  
    
    
    public class Example  
        {  
            public string id { get; set;}  
      
            public string full_name { get; set;}  
        }  
    

    Result:

    275574-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and 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.

    1 person found this answer helpful.
    0 comments No comments

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.