Import data from the text file

AMER SAID 396 Reputation points
2023-02-17T18:28:12+00:00

I have a text file that I got from Google Maps.

I want to import and display data in Datagrid View.

what i want

Untitled2

my code .

 using (var streamReader = new StreamReader(@"D:\googlemap.txt"))
            {
                while (!streamReader.EndOfStream)
                {
                    var line = streamReader.ReadLine();
                    var values = line.Split('\t');
                    var rowIndex = dataGridView1.Rows.Add();
                    for (int i = 0; i < values.Length; i++)
                    {
                        dataGridView1.Rows[rowIndex].Cells[i].Value = values[i];
                    }
                }
            }
Developer technologies | C#
Developer technologies | 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.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Viorel 125.7K Reputation points
    2023-02-19T06:06:48.1133333+00:00

    Check a variant:

    dataGridView1.Rows.Clear( );
    
    int number_of_columns = dataGridView1.Columns.Count;
    int i = 0;
    
    foreach( var line in File.ReadLines( @"D:\googlemap.txt" ) )
    {
        if( i % number_of_columns == 0 ) dataGridView1.Rows.Add( );
    
        dataGridView1[i % number_of_columns, i / number_of_columns].Value = line;
    
        ++i;
    }
    
    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' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.