[SOLVED] Import Data From Text File and Show As DataGrid View Using C#

DataGrid View

If you have text data in a file and want to show it in datagrid view using C#, here is the code.

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;


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>