How to insert a List<> into a Listview C#?

Asked

Viewed 56 times

1

I’m trying to insert a List<> (Generated by another class that brings the data from the Database), the data is inserted but the ListView receives only the first column, and the other is null.

Below is the code of form where the ListView, tried something with the foreach but I was also unsuccessful

private void Form_Usuarios_Load(object sender, EventArgs e)
{
    List<Usuario> usuarios = new List<Usuario>();
    UsuarioDAO usuario = new UsuarioDAO();
    usuarios = usuario.MostrarUsuarios().ToList();
    foreach(var item in usuarios)
    {
        listViewUsuarios.Items.Add(new ListViewItem(new string[] { item.Nome}));
    }  
}

Upshot:

inserir a descrição da imagem aqui

  • You do not put the solution in the question, but in the answer I will reverse it and you put as answer.

2 answers

0

How to insert items into a component Listview of Windows Forms?

In his method ListViewData.Items.Add accepts an instance of the class Listviewitem which shall be loaded with the information contained in a list of values, a basic and manual example:

ListViewData.Items.Clear();
var item0 = new ListViewItem();
item0.Text = "pt.stackoverflow.com";
item0.SubItems.Add("Site");

var item1 = new ListViewItem();
item1.Text = "microsoft.com";
item1.SubItems.Add("Site");

ListViewData.Items.Add(item0);
ListViewData.Items.Add(item1);

where the first column of your Listviewitem on the property Text is the value of the first column of the returned list, and the others are SubItems that with the method Add and added the next text of the next column and so on, the figure represents the code:

inserir a descrição da imagem aqui

for each column to be added plus one SubItems.Add need to be invoked and so on to load all items in that column.

In your code it’s something like this:

private void Form_Usuarios_Load(object sender, EventArgs e)
{   
    UsuarioDAO usuario = new UsuarioDAO();
    List<Usuario> usuarios = usuario.MostrarUsuarios().ToList();
    foreach(var item in usuarios)
    {
        ListViewItem item = new ListViewItem();
        item.Text = item.Nome;
        item.SubItems.Add(item.Cargo);
        listViewUsuarios.Items.Add(item);
    }  
}

References:

0

I got it here guys, below is the code with the changes I made to the code work.

private void Form_Usuarios_Load(object sender, EventArgs e)
{
    List<Usuario> usuarios = new List<Usuario>();
    UsuarioDAO usuario = new UsuarioDAO();
    usuarios = usuario.MostrarUsuarios().ToList();  
    foreach(var item in usuarios)
    {
        ListViewItem lv1 = new ListViewItem(item.Nome);
        lv1.SubItems.Add(item.Cargo);
        listViewUsuarios.Items.Add(lv1);            
    }  
}
  • alignment of text in code format is important, check this at post time.

  • if that return usuario.MostrarUsuarios().ToList(); return a List<Usuario> because before you create an instance that is unnecessary? take a look at the code that I implemented. Beware of unnecessary codes in coding and instances without need is also a problem.

Browser other questions tagged

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