Hallo,

i have a Csv file,i would like to read ,convert it and than represent it with Datagrid

the fonction who read my csv

public List<String[]> Parse_CSV(String File_Path)
        {
            List<string[]> Parse_Data = new List<string[]>();
            try
            {
                using (StreamReader Read_File = new StreamReader(File_Path))
                {
                    string Line;
                    string[] Row;

                    while ((Line = Read_File.ReadLine()) != null)
                    {
                        Row = Line.Split(',');
                        Parse_Data.Add(Row);

                        MessageBox.Show(Parse_Data.ToString());
                        MessageBox.Show(Row.ToString());
                                            }
                }

            }
            catch (Exception e) 
            {

                MessageBox.Show( e.Message);
            
            }

            return Parse_Data;
    
    
    
    
        }

i join a example of my Csv file

thx in advance

use this method
String[] rows = File.ReadAllLines("filelocation");
you will get rows in array

Ok thx, and shoud i make to represent the Rows and line with Datagrid???

Yes you'll get the rows in array you split it by "," and add the rows in datagridview!!

File.ReadAllLines("filelocation");???? you mean File.ReadToEnd("filelocation");???? i haave never fund your File.ReadAllLines

take a look at this link.. Use namespace Using.System.IO;

But in my case File ist a StreamReader,and don´t acces to this methode ReadAllLines.
report to my code,

string[] Row = Read_File.ReadAllLines(File_Path);

check it my compiler give a fehler back

Ok it is good,that was my mistake thank you....it is possible

No i still have problem with the representation on a Datagrid,may be you can help me

This is code I use in one of my programs:

// read CSV file from LIMS and make a Datatable Object 
        public DataTable BuildDataTable(string fileFullPath, char seperator)
        {
            const int EOF = -1;

            DataTable myTable = new DataTable("MyTable");
            DataRow myRow;
            StreamReader myReader;
            string Title;
            try
            {
                myReader = new StreamReader(fileFullPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fout bij openen bestand: " + ex.Message);
                return new DataTable("Empty");
            }
            try
            // Open file and read first line to determine how many fields there are.
            {
                string[] fieldValues = myReader.ReadLine().Split(new Char[] { seperator, '\t' }); //skip title
                Title = fieldValues[0];
                fieldValues = myReader.ReadLine().Split(new Char[] { seperator, '\t' });
                // Adding the first line of data to data table (columnnames)
                // Create data columns accordingly
                for (int i = 0; i < fieldValues.Length; i++)
                {
                    if (i==0) myTable.Columns.Add(new DataColumn("Code"));
                    else  if (i==1) myTable.Columns.Add(new DataColumn("Datum"));
                    else
                        myTable.Columns.Add(new DataColumn(fieldValues[i].ToString().Trim()));
                }
                //Now reading the rest of the data to data table
                while (myReader.Peek() != EOF)
                {
                    fieldValues = myReader.ReadLine().Split(new Char[] { seperator });
                    myRow = myTable.NewRow();
                    for (int i = 0; i < fieldValues.Length; i++)
                    {
                        myRow[i] = fieldValues[i].ToString().Trim();
                    }
                    myTable.Rows.Add(myRow);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error building datatable: " + ex.Message);
                return new DataTable("Empty");
            }
            finally
            {
                if (myReader != null)
                {
                    myReader.Close();
                }
            }
            myTable.AcceptChanges();
            if (myReader != null)
            {
                myReader.Close();
            }
            return myTable;
        }

Once you have a DataTable use:
MyDataGridView.DataSource = MyDataTable;

hey ddanbe,
i modify your code an aplain it in the file i have joined in my Thread,but it never run,i got a Exception(Erro Building Datatable:a Row '0' is already be in this Datatable.)
Can you help me please????

thx

In my CSV file I had a title, which I don't need in the DGV in this case. Try to remmove Line 22,23 of my code. Hope it helps.

i made it but without Succes, but i still looking for another Solution


Thx ddanbe

What do do you mean by: "I made it but without success" ?
My code was meant for the situation I had to handle, think you can adapt it to yours.

Hey Ddanbe ,th way i try to explain ist that,in my Datagid,my rows don´t contain any values,i have only values in Colums,and in Rows Nothing.that is the way i modify your code,just loók please and tell me what i made wrong,thx in advance

public DataTable BuildDataTable(string fileFullPath, char seperator)   
        {  const int EOF = -1;
            DataTable myTable = new DataTable("MyTable");            
            DataRow myRow;            
            StreamReader myReader = new StreamReader(fileFullPath);            
            string Set_Data = myReader.ReadLine();  
           
            int  Set_len =  Set_Data.Length;
            try            
            {               
               myReader = new StreamReader(fileFullPath);            
            }            
            catch (Exception ex)            
            {                
                MessageBox.Show("Fout bij openen bestand: " + ex.Message);              
                return new DataTable("Empty");          
            }           
            try            
                // Open file and read first line to determine how many fields there are.            
            {
                string[] fieldValues =  new string [Set_len];//myReader.ReadLine().Split(new Char[] { seperator, '\t' }); 
                //skip title               
               // Title = fieldValues[2];               
                fieldValues = myReader.ReadLine().Split(new Char[] { seperator, '\t' });                
                // Adding the first line of data to data table (columnnames)                
                // Create data columns accordingly                
                for (int i = 0; i < fieldValues.Length; i++)               
                {                    
                    //if (i==0) myTable.Columns.Add(new DataColumn("Code"));                   
                 
                     //if (i==1) myTable.Columns.Add(new DataColumn("Datum"));                  
                     myTable.Columns.Add(new DataColumn(fieldValues[i].ToString().Trim()));              
                }               
                //Now reading the rest of the data to data table               
                while (myReader.Peek() != EOF)               
                {                   
                    fieldValues = myReader.ReadLine().Split(new Char[] { seperator });                  
                    myRow = myTable.NewRow();                    
                    for (int i = 0; i < fieldValues.Length; i++)                   
                    {                      
                        myRow[i] = fieldValues[i].ToString().Trim();
                       
                    }
                    myTable.Rows.Add(myRow);         
                }          
            }           
            catch (Exception ex)           
            {               
                MessageBox.Show("Error building datatable: " + ex.Message);               
                return new DataTable("Empty");           
            }            
            finally         
            {               
                if (myReader != null)               
                {                  
                    myReader.Close();               
                }            
            }            
            myTable.AcceptChanges();           
            if (myReader != null)          
            {               
                myReader.Close();           
            }
            dataGridView1.DataSource = myTable;
            return myTable;       
        }

i have only values in Colums,and in Rows Nothing

Could you give an example of your CSV file?
Also, my method returns a DataTable, so line 64 should happen outside the method.

in my thread was an example von my csv file als joined thx

OK, this is what I came up with, hope it helps.
Started a forms app, dropped in a datagridview and a button.
Here is the code and a screenshot:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ProcesFile();
        }

        public void ProcesFile() // use this code in a button or menu or both
        {
            DataTable DT;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                DT = BuildDataTable(openFileDialog1.FileName,'\t');
                this.dataGridView1.DataSource = DT;
            }
        }

         public DataTable BuildDataTable(string fileFullPath, char seperator)        
         {            
             const int EOF = -1;             
             DataTable myTable = new DataTable("MyTable");            
             DataRow myRow;            
             StreamReader myReader;            
             try            
             {                
                 myReader = new StreamReader(fileFullPath);            
             }            
             catch (Exception ex)            
             {                
                 MessageBox.Show("Fehler beim ofnen: " + ex.Message);   // :o)             
                 return new DataTable("Empty");            
             }
             try            
                 // Open file and read first line to determine how many fields there are.            
             {
                 string[] fieldValues = myReader.ReadLine().Split(new Char[] { seperator });                
                 // Adding the first line of data to data table (columnnames)                
                 // Create data columns accordingly                
                 for (int i = 0; i < fieldValues.Length; i++)                
                 {                                          
                    myTable.Columns.Add(new DataColumn(fieldValues[i].ToString().Trim()));  
                 }
                 //Now reading the rest of the data to the data table                
                 while (myReader.Peek() != EOF)                
                 {                    
                     fieldValues = myReader.ReadLine().Split(new Char[] { seperator });          
                     myRow = myTable.NewRow();                    
                     for (int i = 0; i < fieldValues.Length; i++)  //fill a row with values                  
                     {                        
                         myRow[i] = fieldValues[i].ToString().Trim();                    
                     }                    
                     myTable.Rows.Add(myRow);                
                 }            
             }
             catch (Exception ex)
             {
                 MessageBox.Show("Error building datatable: " + ex.Message);
                 return new DataTable("Empty");
             }
             finally
             {
                 if (myReader != null)
                 {
                     myReader.Close();
                 }
             }
             myTable.AcceptChanges();
             if (myReader != null)
             {
                 myReader.Close();
             }
             return myTable;
         }
    }
}

Happy computing!

Danke sehr Ddanbe it´s runing