C# ListView repeating 1 column from csv

Importing .csv into a ListView in c# duplicates the first entry.

Original csv file: Original file image

What I get: Visual Studio Error

I am trying to upload the .csv to the list, so if everything validates correctly, the table fills and I would then pass that data to my database.

private void btn_Cargar_Click(object sender, EventArgs e)
//button load .csv file click
{
    list_Previ.Items.Clear();
    filepath = "";
    txtbox_ArchivoCargado.Text = "";
    cargarCSV();   
}

private void cargarCSV() //Load .csv 
{
    OpenFileDialog dialogoCargar = new OpenFileDialog();
    dialogoCargar.Filter = "Archivos CSV|*.csv";
    dialogoCargar.FilterIndex = 1;
    if(dialogoCargar.ShowDialog() == DialogResult.OK)
    {
        filepath = dialogoCargar.FileName;
        txtbox_ArchivoCargado.Text = filepath;
    }
}

private void btn_Validame_Click(object sender, EventArgs e) //Validate Button Click
{
    if(filepath == "") MessageBox.Show("No hay nada que validar\nPuedes empezar cargando un archivo", "Corporativo Acosta | Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    if(filepath != "")
    {
        Validar();
    }
}

private void Validar() //Validate
{
    empleadosValido = true;
    try {
        FileStream fileStreamNew = File.Open(filepath, FileMode.Open, FileAccess.ReadWrite);
        StreamReader streamRead = new StreamReader(fileStreamNew);
        string strView = streamRead.ReadToEnd();
        streamRead.Close();
        fileStreamNew.Close();
        String[] strArray = strView.Split(new char[] { ',' });
        ListViewItem item = new ListViewItem(strArray[0].ToString());

        item.SubItems.Add(strArray[0].ToString());
        item.SubItems.Add(strArray[1].ToString());
        item.SubItems.Add(strArray[2].ToString());
        item.SubItems.Add(strArray[3].ToString());
        item.SubItems.Add(strArray[4].ToString());
        item.SubItems.Add(strArray[5].ToString());
        item.SubItems.Add(strArray[6].ToString());

        list_Previ.Items.Add(item);

    }
    catch (Exception ex)
    {
        if (ex is IOException)
        {
            MessageBox.Show("El archivo se encuentra en uso por otro programa\nPor favor cierra otros programas e intenta de nuevo.", "Corporativo Acosta | Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            empleadosError = true;
            MessageBox.Show(ex.ToString());
        }
        if (ex is IndexOutOfRangeException)
        {
            MessageBox.Show("Hay un problema con tus columnas.\nVerifica que correspondan las columnas a importar\ncon las de la tabla (7 columnas)", "Corporativo Acosta | Error", MessageBoxButtons.OK ,MessageBoxIcon.Error);
            empleadosError = true;
            MessageBox.Show(ex.ToString());
        }

    }
}
Jon Skeet
people
quotationmark

I suspect this is the problem:

ListViewItem item = new ListViewItem(strArray[0].ToString());

item.SubItems.Add(strArray[0].ToString());

You're adding the first value twice, once as the "main" item and once as a subitem. Try just removing that second line.

From the View.Details documentation:

Each item appears on a separate line with further information about each item arranged in columns. The left-most column contains a small icon and label, and subsequent columns contain sub items as specified by the application. A column displays a header which can display a caption for the column. The user can resize each column at run time.

So that does sound like the issue - the first displayed column showing the value you pass to the ListViewItem constructor.

As an aside, you've already got a string array, so you don't need all those ToString calls:

ListViewItem item = new ListViewItem(strArray[0]);

item.SubItems.Add(strArray[1]);
item.SubItems.Add(strArray[2]);
item.SubItems.Add(strArray[3]);
item.SubItems.Add(strArray[4]);
item.SubItems.Add(strArray[5]);
item.SubItems.Add(strArray[6]);

Or using an object initializer:

ListViewItem item = new ListViewItem(strArray[0])
{
    SubItems = { strArray[1], strArray[2], strArray[3], 
                 strArray[6], strArray[5], strArray[6] };
};

people

See more on this question at Stackoverflow