skip to Main Content

The data is not displayed correctly in the columns.
The CSV consists of 7 columns. Rows are of different length.
I can not upload a picture.(https://ibb.co/0fnfLW7)

        DataTable tblcsv = new DataTable();
        tblcsv.Columns.Add("Vorname");
        tblcsv.Columns.Add("Nachname");
        tblcsv.Columns.Add("RFID");
        
          string csvData = File.ReadAllText(csvPath);
        //spliting row after new line  
        foreach (string csvRow in csvData.Split(';'))
        {
            if (!string.IsNullOrEmpty(csvRow))
            {
                //Adding each row into datatable  
                tblcsv.Rows.Add();
                int count = 0;
                foreach (string FileRec in csvRow.Split(';'))
                {
                    tblcsv.Rows[tblcsv.Rows.Count - 1][count] = FileRec;
                    count++;

                    for(var x=0; x<7; x++)
                    {
                        //tblcsv[x][count] = FileRec;
                    }
                    count++;
                }
            }
            //Calling Bind Grid Functions  
            BindgridStaffImport(tblcsv);
        }

2

Answers


  1. Chosen as BEST ANSWER

    Thank you very much for the support. I now have a working variant. However, the goal is to skip the first line, the header. Are there any ideas how I can implement this.

      List<string> headercolumns = new List<string> { "Vorname", "Nachname",  };
                    DataTable dt = new DataTable();
                    foreach (string header in headercolumns)
                    {
                        
                        dt.Columns.Add(header);
                    }
    
                    List<string> StaffList = new List<string>();
                    using (StreamReader filereader = new StreamReader(StaffFileUpload.FileContent))
                    {
                        
                        string readline = string.Empty;
                        while ((readline = filereader.ReadLine()) != null)
                        {
                           
                            StaffList.Add(readline);
                        }
                        if (StaffList != null && StaffList.Count > 0)
                        {
                            foreach (string info in StaffList)
                            {
                                DataRow row = dt.NewRow();
                                string[] mitarbeiter = info.Split(';');
    
                                for (int i = 0; i < mitarbeiter.Length; i++)
                                {
                                    row[i] = mitarbeiter[i];
                                }
                                dt.Rows.Add(row);
                            }
                        }
                    }
    

  2. You split code is using ";", and should it not be "," if a csv?

    The code will look much like this:

            foreach (string csvRow in csvData.Split(';'))
            {
                if (!string.IsNullOrEmpty(csvRow))
                {
                    //Adding each row into datatable  
                    DataRow MyNewRow = tblcsv.NewRow();
                    int count = 0;
                    foreach (string FileRec in csvRow.Split(','))
                    {
                        MyNewRow[count] = FileRec;
                        count++;
                    }
                    tblcsv.Rows.Add(MyNewRow);
                }
                //Calling Bind Grid Functions  
                BindgridStaffImport(tblcsv);
    

    So, use the "newRow" method to create a new blank row based on the table. Set the column values, and then add this new row to the rows collection of the table.

    And even better is to use the TextFieldParser class.

    So,

    using Microsoft.VisualBasic.FileIO;
    

    And then you can even assume say the first row has the colum names.

    This:

    We assume you read the csv file into stirng sFileLocal

            // process csv file
            TextFieldParser MyParse = new TextFieldParser(sFileLocal);
            MyParse.TextFieldType = FieldType.Delimited;
            MyParse.SetDelimiters(new string[] { "," });
            MyParse.HasFieldsEnclosedInQuotes = true;
    
            // add colums to table.
    
            DataTable rstData = new DataTable();
            foreach (string cCol in MyParse.ReadFields())
            {
                rstData.Columns.Add(cCol);
            }
            while (!MyParse.EndOfData)
            {
                string[] OneRow = MyParse.ReadFields();
                rstData.Rows.Add(OneRow);
            }
            // Now display results in a grid
            GridView1.DataSource = rstData;
            GridView1.DataBind();
    

    Quite much all above is you need.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search