0

I need to declare 32 string arrays containing arrays in C#. Writing it all out works OK similar to this:

string[] row1 = new string[] { "NO.", "DATA BIN", "BIT2", "BIT1", };
string[] row2 = new string[] { "1", DataBin[1], BitLabels[1, 1], BitLabels[0, 1], };
string[] row3 = new string[] { "2", DataBin[2], BitLabels[1, 2], BitLabels[0, 2], };
string[] row4 = new string[] { "3", DataBin[3], BitLabels[1, 3], BitLabels[0, 3], }; 

But it would be so much cleaner / easier if I could create them like this:

string[] row1 = new string[] { "NO.", "DATA BIN", "BIT2", "BIT1", };
for (int i = 2; i < 33; i++)
{
string[] row(i) = new string[] { Convert.ToString(i), DataBin[i], BitLabels[1, i], BitLabels[0, i], };
}

The problem is that I can't index the instance name (row1 for example)

This is for a DataGridView, so I also need to have something to take care of the following:

object[] rows = new object[] { row1 , row2 , row3 , row4 , row5 , row6 , row7 , row8 , row9 ,
                               row10 , row11 , row12 , row13 , row14 , row15 , row16 , row17 ,
                               row18 , row19 , row20 , row21 , row22 , row23 , row24 , row25 ,
                               row26 , row27 , row28 , row29 , row30 , row31 , row32 , row33 };            
foreach (string[] rowArray in rows)
        {
            myDataGridView.Rows.Add(rowArray);
        }

Any suggestions? (sorry many edits for clarity)

1
  • You have the wrong concept about how DGV columns and headers work. Commented Aug 14, 2013 at 21:00

4 Answers 4

5

You can use a multidimensional ([,]) or a jagged ([][]) array.

Based on your example I would go for jagged (array of array):

string[][] rows = new string[32][];

for (int i = 0; i < rows.Length; i++)
{
    int n = i+1;  // you seem to use 1 as origin
    rows [i] = new string[] { n.ToString(), DataBin[n], ... };
}

Edit: This is for a DataGridView, so ...

So you can't actually use an array (for the columns), you will need a proper class. With property-names to bind to. You don't need array for the rows either (to restrained).

An easy way to get from where you are to where you need to be is to use an anonymous class:

(Borrowing a piece from Tim)

var rows = Enumerable.Range(1, 32)
     .Select(i => new {   // this is the anon class
         Index = i.ToString(),   // on-the-fly property 'Index'
         Text1 = DataBin[i], 
         Text2 = BitLabels[2, i], 
         Text3 = BitLabels[1, i],  /* etc */ }
       );

And then you can bind your columns to Index, Text1, Text2, etc

Sign up to request clarification or add additional context in comments.

2 Comments

Why? That is what you seem to need here. Only 2 levels though.
They are properties, you'll need something like column1.DataField="Index";
3

Maybe you want to use Linq:

string[][] rows = Enumerable.Range(1, 32)
    .Select(i => new [] { i.ToString(), DataBin[i], BitLabels[2, i], BitLabels[1, i], BitLabels[0, i], })
    .ToArray();

Comments

2

You could use a List<string[]>, for sample:

var rows = new List<string[]>();

for (int i = 0; i < 32; i++)
{
   rows.Add(new string[] { Convert.ToString(i), DataBin[i], BitLabels[2, i], BitLabels[1, i], BitLabels[0, i] });
}

After it, could use the rows object and access by

rows[0][3]

Where the first index is a item of the List, and second index is the index of the array.

You also could call the ToArray() and get a array of string[].

Comments

1

It looks like you've almost got it. The array answer is a good one. Another possible approach would be to use a list. You can convert the list to an array later if needed. As you can see the code is clean and easy to follow.

var rows = new List<string[]>();
for (int i = 0; i < 32; i++)
{
    var row = new string[] { Convert.ToString(i), DataBin[i], BitLabels[2, i], BitLabels[1, i], BitLabels[0, i], };
    rows.Add(row);
}

// use rows.ToArray() if you need it to be an array or arrays later.

1 Comment

This solution worked great with a tiny bit of tweaking (I decided not to use var). myDataGridView.Rows.Add(new string[] { "NO.", "DATA BIN", "BIT1", "BIT0", }); for (int i = 1; i < 33; i++) { myDataGridView.Rows.Add(new string[] { Convert.ToString(i), DataBin[i], BitLabels[1, i], BitLabels[0, i], }); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.