-1

I am developing the Project where I have to read multiple CSV file and view in the DataGrid. Now here challenge is if there is 5 files I have to create 5 different tab to view in Datagrid which I am not able to do. Here is my code :

       DataSet ds = new DataSet();
        foreach (String file in openFileDialogCSVFilePath.FileNames)
        {
            string fileLocation = file;
            string fileName = Path.GetFileName(@fileLocation);
            CsvReader reader = new CsvReader(fileLocation);
            ds = reader.RowEnumerator;
        }
        dGridCSVdata.DataSource = ds;
        dGridCSVdata.DataMember = "The Data";

Can anybody help me how I can accomplish this? Thanks in advance.

UPDATE:

I want to create Tab pro grammatically.

5
  • Possible duplicate of How can I manually add data to a dataGridView? Commented Oct 29, 2018 at 12:20
  • @Anas I have already tried that, It didnt worked in my case Commented Oct 29, 2018 at 12:25
  • So you have to create Tab Page programmatically and add DatagridView in each Tab based on total files available.
    – SH7
    Commented Oct 29, 2018 at 12:31
  • @SH7 Exactly. That what I am looking for. Commented Oct 29, 2018 at 12:32
  • create 5 different tab to view in Datagrid which I am not able to do - You fail to tell us just what is/isn't working. Do note that stuff you do to a hidden tabpage will not automitically get updated!
    – TaW
    Commented Oct 29, 2018 at 12:54

1 Answer 1

0

Here is a code snippet . So i have a TabControl tabControl1with One TabPage.

Step 1: Read and Get total Number of Files and Store the File Path in List<string>

Step 2: Based on the Count of File Generate the TabPage.

Step 3: Read CSV File and Store it in Datatable

Step 4: For the TabPage add the DataGridView and map its DataSource to the DataTable

Please note that this isn't tested.

private void frmMain_Load(object sender, EventArgs e)
    {
        //Get Total Number of File to create Total Tab
        //Pass the count in the for loop
        //Store the File Path of Each File in List<string> lstFilePath

        for (int i = 0; i < 5; i++) //i< lstFilePath.Count
        {
            //Create Tab Programatically
            this.tabControl1.TabPages.Add("Tab Page"+ (i+1).ToString());                

            //Create DataTable to read and Store the CSV File Data
            DataTable Dt = new DataTable();

            //Based on the i value get the File Path from lstFilePath 
            //and pass it to Function below

            Dt = ConvertCSVtoDataTable("");               
            DataGridView grid = new DataGridView();
            this.tabControl1.TabPages[i].Controls.Add(grid);
            grid.DataSource = Dt;
        }
    }

    public static DataTable ConvertCSVtoDataTable(string strFilePath)
    {
        DataTable dt = new DataTable();
        using (StreamReader sr = new StreamReader(strFilePath))
        {
            string[] headers = sr.ReadLine().Split(',');
            foreach (string header in headers)
            {
                dt.Columns.Add(header);
            }
            while (!sr.EndOfStream)
            {
                string[] rows = sr.ReadLine().Split(',');
                DataRow dr = dt.NewRow();
                for (int i = 0; i < headers.Length; i++)
                {
                    dr[i] = rows[i];
                }
                dt.Rows.Add(dr);
            }

        }

        return dt;
    }
2
  • thank you for your response. Can you help me what is here tabControl1 Commented Oct 29, 2018 at 13:00
  • Have edited the answer, have mentioned the steps too. The code isn't tested but i guess you could use the Part where i'm programmatically adding the TabPage and DataGridView and Populating DataTable from csv to DataGridView.
    – SH7
    Commented Oct 29, 2018 at 13:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.