How to make a two-dimensional arrangement of String in C #?

0

I have a small problem, I hope and you can support me;

The problem is that I want to make a 2-dimensional arrangement to make a table, something like this:

                    string[][] dat = new string[][]
                {
                    new string[] {"Tienda"     ,"Existencias"},
                    new string[] {"Argentina","1"},
                    new string[] {"México","5"},
                    new string[] {"Brazil","9"},
                    new string[] {"Canada","2"}

                };

As you can see, there are 2 columns and several rows, the columns are always going to be 2 but the rows may vary.

To be moving the array indexes and and filling them with for , this is the code that I have and try but only the last element that gives true in if is saved;

Store[] tienda = p_item;
for (int c = 0; c < tienda.Length; c++)
        {
            if (tienda[c].stockOnHand > 0)
            {
                string[][] dat = new string[][]
                {
                    new string[] {tienda[c].storeName ,tienda[c].stockOnHand.ToString()}
                };

                v_dataTable = dat;
            }
            else
            {

            }
        }

Try putting a variable that was increasing, but it shows me an error: "Error 1 A constant value is expected"

string[][] dat = new string[][]
                {
                    new string[numero] {tienda[c].storeName ,tienda[c].stockOnHand.ToString()}

                };
                numero++;

Is there any way to do something like this?

String[][] dat = new String[][];
dat[c][1]={"tienda[c].storeName ,tienda[c].stockOnHand.ToString()"};

To fill the positions according to the position of the arrangement and to make the table.

Do you have any other idea of how to fill that arrangement?

    
asked by roboto10 30.10.2018 в 22:49
source

2 answers

0

At least the first dimension should indicate it, something like this

Store[] tienda = p_item;

string[][] dat = new string[tienda.Length][];

for (int c = 0; c < tienda.Length; c++)
{
    if (tienda[c].stockOnHand > 0)
    {
        dat[c] = new string[] {tienda[c].storeName ,tienda[c].stockOnHand.ToString()}
    }

}

analyze the documentation of how this type of array works

Jagged Arrays (C # Programming Guide)

But I really would not recommend using this type of array, use lists and classes, that is, define

public class Tienda
{
    public string storeName {get;set;}
    public string stockOnHand {get;set;}
}

and then you use linq

List<Tienda> dat = p_item.Where(x=>x.stockOnHand > 0)
                        .Select(new Tienda()
                        {
                            storeName = x.storeName,
                            stockOnHand = x.stockOnHand.ToString()
                        }});

So easy, you have a list that will be the rows and the entity Tienda has the two properties that will be the columns

    
answered by 30.10.2018 в 23:19
0

I already found how to solve it and it is that a class was used to make the table but I decided to do it directly and that's how it worked, I'll leave it as if it's useful for someone

  DataTable dt = new DataTable();
        dt.Columns.Add("Tienda", typeof(string));
        dt.Columns.Add("Existencias", typeof(string));

        for (int c = 0; c < tienda.Length; c++)
        {
            if (tienda[c].stockOnHand > 0)
            {
                dt.Rows.Add(new object[] { tienda[c].storeName, tienda[c].stockOnHand.ToString()});
            }
            else
            {

            }
        }ódigo aquí
    
answered by 30.10.2018 в 23:26