How to add columns and values in the columns in a ListView at run time?

2

I have ListView listViewListaChamada and I'm trying to add columns and values to the columns corresponding to my ListView listViewListaChamada , but I did not succeed. I created the function below to popular my ListView:

private void PopulaListView(MListaPresenca lista_chamada, ListView listview, MAluno aluno) 
{
    listview.Items[0].SubItems.Add(aluno.Nome);
    listview.Items[1].SubItems.Add(lista_chamada.Presente);
}

Where the first column receives the nome of the student and the second column receives the value of the Presente attribute whose value can be SIM or NÃO , so my ListView will have two columns, however, at the moment of entering the values I get the following error:

  

"InvalidArgument = Value '0' is not a valid value for 'index'. \ r \ nName   of the parameter: index "

I would like to know how to add columns and values in the columns of my ListView at run time?

    
asked by anonymous 16.12.2015 / 21:02

1 answer

1

First I had to create the columns:

listViewListaChamada.View = View.Details;
listViewListaChamada.Columns.Add("Aluno", 490);
listViewListaChamada.Columns.Add("Presente", 100);

I set the above routine in the Load event of my form.

Then I made the modification in the method PopulaListView , see how it was:

private void PopulaListView(ListView listview, string nome,  string presente) 
{
    ListViewItem item = new ListViewItem(new []{nome, presente});    
    listview.Items.Add(item);
}

Only the method call was passed by passing the parameters and I was successful.

Font .

    
17.12.2015 / 18:43