How to add columns and values in columns in a Listview at runtime?

Asked

Viewed 1,720 times

2

I own a ListView listViewListaChamada and I am trying to add columns and values to the columns corresponding to my ListView listViewListaChamada, however 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 attribute Presente the value of which may be SIM or NÃO, so mine ListView will be left with two columns, however, when entering the values I receive the following error:

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

I would like to know how to add columns and values in my columns ListView at runtime?

1 answer

1


First I had to create the columns:

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

Set the above routine in the event Load of my form.

Then I modified the method PopulaListView, see how it turned out:

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

You only called the method by passing the parameters and I was successful.

Source.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.