Hi,
I am trying to run an application which is supose to present a CSV file contents in a DataGridView.The problem is the csv file contains a line (Fiist Line) which is the Title of CSV like:

================================
Title: Titanic
Director,Year,Rate,Star1,Star2
James Cameron,1997,7.5,Leonardo DiCaprio,Kate Winslet

================================
and here is my code

private void btnLoadData_Click(object sender, EventArgs e)
{
    string rowValue;
    string[] cellValue;
   if (System.IO.File.Exists(txtPath.Text))
    {
        System.IO.StreamReader streamReader = new StreamReader(txtPath.Text);

        // Reading header
        rowValue = streamReader.ReadLine();
        cellValue = rowValue.Split(',');                
        for (int i = 0; i <= cellValue.Count() - 1; i++)
        {
            DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
            column.Name = cellValue[i];
            column.HeaderText = cellValue[i];
            dataGridView1.Columns.Add(column);
        }

        // Reading content
        while (streamReader.Peek() != -1)
        {
            rowValue = streamReader.ReadLine();
            cellValue = rowValue.Split(',');
            dataGridView1.Rows.Add(cellValue);
        }

        streamReader.Close();
    }
    else
    {
        MessageBox.Show("No File is Selected");
    }
}

1- Can you please let me know how I can skip exporting the fist line (Title: Titanic) into the DataGridView?
2- How I can retrive the Fist line and Just the Title (Titanic) and not the "Title:" to use somehwre else?

Thanks for your time and support

Dani AI

Generated

— your file has a small decorative block before the real CSV header, so you should explicitly consume those lines, extract the Title, then locate the true header row. is right that skipping lines works for a fixed file, but a slightly more robust approach will handle blank lines, separator lines (====), different capitalization, and let you safely grab the title text (without "Title:") before creating columns.

A simple, reliable flow:

  • Read lines until you find a line that contains the Title label; take everything after the first ':' and call Trim() to get "Titanic".
  • Continue reading until you hit a candidate header line (a non-empty line that contains commas and is not a label/separator).
  • Use that header to create DataGridView columns, then parse subsequent rows.

Example extraction snippet:

string title = null;
string headerLine = null;
string line;
while ((line = reader.ReadLine()) != null)
{
    line = line.Trim();
    if (line.Length == 0 || line.StartsWith("===")) continue;
    int colon = line.IndexOf(':');
    if (colon > 0 && line.Substring(0, colon).Equals("Title", StringComparison.OrdinalIgnoreCase))
    {
        title = line.Substring(colon + 1).Trim();
        continue;
    }
    if (line.Contains(","))
    {
        headerLine = line;
        break;
    }
}

Notes and cautions: Split(',') fails on quoted fields containing commas — use Microsoft.VisualBasic.FileIO.TextFieldParser or a CSV library (CsvHelper) for production data. Trim header names and check for duplicate column names before adding to the grid. Use the extracted title (for example, set Form.Text or a label) and only then process the remaining records.

rowValue = streamReader.ReadLine();

If you just read a line (like at code-line 20) before the loop, it will skip the first line in the file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.