Table Completion with List

Asked

Viewed 1,035 times

0

Is there any function in Visual Studio where it is possible to fill a table with values of a List<> without a specified number of positions?

  • 2

    Explain your question better. What are we talking about? What have you done? What is undetermined?

  • 2

    What do you mean by a table? A Gridview? A Listview? What framework do you use?

  • i have several values stored in List<>'s, and I want to present this is a Windows Form, but n I want to put in a Richtextbox for example pq is all loose, so I want to present in a table to be organized, but in the table comes pre-rows and columnsdefined, and I don’t know how to make the visual go creating lines as it advances in the positions of the List

  • I believe that using WPF instead of Winforms is much easier to do this

  • Thanks for everyone’s attention, but I managed using a datatable by adding the values of List<>s in columns and then adding to datagridview, worked perfectly and was well organized

2 answers

1

What you need to do is use the LINQ feature to create a new anonymous type and then fill in Datagridview with the anonymous type you created.

List<Usuario> lstUsr = preencheLstUsr();
var newList = lstUsr.Select(usuario => new
{
    Id = usuario.id,
    Nome = usuario.login,
    LoginNome = usuario.login,
    PerfilDescricao = usuario.perfil.descricao
}).ToList();

The method would look like this:

private void btnPreencheGrid_Click(object sender, EventArgs e)
{
    List<Usuario> lstUsr = preencheLstUsr();
    var novaListUsuario = lstUsr.Select(usuario => new
        {
            Id = usuario.id,
            Nome = usuario.login,
            LoginNome = usuario.login,
            PerfilDescricao = usuario.perfil.descricao
        }).ToList();


    dgv.DataSource = null; //Limpa o grid;
    dgv.DataSource = novaListUsuario;
    dgv.Refresh();
}

For this change to work it is necessary to edit the Datagridview and change Datasource to (None) in the editor, as well as edit each one of the columns belonging.

That is, in each of the columns, change the Datapropertyname to match the name of the property in the created anonymous type with which belongs popular to column.

That answer is based on in that other, so I used as an example List<Usuario> with those attributes (Id,Nome,LoginNome,PerfilDescricao). But it’s just an example you can adapt to your reality.

0

you can assign a List directly to the Datasource of a Gridview, but whenever you modify your List you will have to force a Gridview update.

So instead, I recommend using a Bindinglist.

BindingSource bindingSource = new BindingSource();
BindingList<T> bindingList = new BindingList<T>(lista);
bindingSource.DataSource = bindingList;
gridView.DataSource = bindingSource;

Where list is yours List<T>, T the type listed in the list, and gridView your Gridview.

Once Binding is done, you start to manipulate the bindingList instead of your lista, if you want to monitor which change in the bindingListcan implement the event BindingList<T>.ListChanged.

Browser other questions tagged

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