Access a specific row and column from a C#list

Asked

Viewed 1,419 times

1

How do I make a multidimensional list, and that I can add elements like this:

List<List<String>> listaMultidimensional = new List<List<string>>();

listaMultidimensional[0][0].Add ("maca");
listaMultidimensional[0][1].Add ("banana");
listaMultidimensional[1][3].Add ("limão");

What I can do is:

listaMultidimensional[0].Add("maca");

This would be to add "stretcher" in the first row in the first column, but how do I add "stretcher" in the second column ([0][1]) for example? And then how to access the data from a specific row and column of that list?

  • In this case I recommend that you use Datatable.

  • This doesn’t make much sense, try to improve your question, show what you’re trying to do, show where you’re going.

  • I understood what he meant, take the pendant off.

  • 1

    You can understand yes, it’s simple what I asked, I don’t think I would need an example.

  • 1

    With the accepted answer it is more evident that it is not clear. The question speaks in list, the answer speaks in DataTable, are things so different that only by divination it would be possible to answer this. To tell you the truth I think the answer doesn’t even answer what he really wanted, as he’s still learning he’s clinging to what was posted. The question together with the answer will bring difficulties to a person who is learning who will find that list and DataTable it’s all the same.

  • I also understood your question and would answer with one more option if it had not been closed :-/ Datatable is a complete object structure, especially useful for allowing data editing in user controls and sending these changes back to the database, because its lines store versions of the data, it supports constraints, relationships with other datatables, notifies editing events, etc. Another solution option would be for you to make your own data structure, with a search feature, something simpler than Datatable.

  • I initially wanted to use the list, but it would be more complicated using the list, if there is a better way I do not see why not use. Someone coming to do this for a list, will find the topic and know that with Datatable would be best option in this case.

  • And how would that other option?

  • @Moribundochat With the question closed can not answer.

  • ta open now.

  • @Moribundochat It’s not about you marking an answer as accepted or not :D I would answer even if you already accepted one. The problem is that your question was closed by the site moderator. Perhaps if you make an edit it can be reopened. Try to edit the question and mention the name of the columns ("name, color, type...") or whatever... I don’t know what the moderator didn’t understand.

  • Speak for me mp, here is private msg? not even seen kkkkk If there is another way, I want to learn too. It is always good to learn several ways to solve the same problem.

  • @Moribundochat, I would use a completely different shape. I would create a class with the attributes you need. Then just create a List of this class and add the options you need. Dai to recover the specific positions have several forms, but I would use lambda expressions. With the question closed we can not present the example. Good luck.

Show 8 more comments

1 answer

4


I made a very good example here with Datatable:

// Creating the table.

DataTable table = new DataTable("Alimentos");

// Adicionando as colunas e seus respectivos tipos de dados.
table.Columns.Add("Nome").DataType = typeof(string);
table.Columns.Add("Cor").DataType = typeof(string);
table.Columns.Add("Tipo").DataType = typeof(string);
table.Columns.Add("Cor da folha").DataType = typeof(string);
table.Columns.Add("Aparência").DataType = typeof(string);
table.Columns.Add("Classificação").DataType = typeof(string);

// Adicionando as linhas.
table.Rows.Add("maca", "vermelha", "fruta", "verde", "bonita", "natural");

//Printando as linhas.
for(int i = 0; i < table.Rows.Count; i++)
{
    if((table.Rows[i][0] as string).Contains("maca")) // Use "as string" pois o table.Rows[i][0] retorna um objeto, e você sabe que o objeto é uma string.
    {
        Console.WriteLine("Nome: " + table.Rows[i][0]);
        Console.WriteLine("Cor: " + table.Rows[i][1]);
        Console.WriteLine("Tipo: " + table.Rows[i][2]);
        Console.WriteLine("Cor da folha: " + table.Rows[i][3]);
        Console.WriteLine("Aparência: " + table.Rows[i][4]);
        Console.WriteLine("Classificação: " + table.Rows[i][5]);
    }
}
  • Wow, thanks, I’m going to study Datatable here now, I’ve never used it, but I believe that’s exactly what I need.

  • Datatable does not have Contains, is there something similar to look for a value in the tables? something like contains that I look for maca, and it finds maca.

  • Yes, I’ll edit it okay?

  • Okay, if you don’t know what the strings are for, don’t worry, in time you will probably understand.

  • Look, this time you left some information missing, I only understood your comment because I saw your other post, the one improved after ok?

  • Thank you again, did you not understand the comment or the question? I will try to improve here.

Show 1 more comment

Browser other questions tagged

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