0

I have some code which loops through my data table and i used the item variable well. But my problem is i needs to iterate through, it needs to be + 1 from the last number it was on. It needs to come out of its loops once the foreach has finished.

private void button2_Click(object sender, EventArgs e)
{
    CreateExcelDoc excell_app = new CreateExcelDoc();
    //creates the main header

    COLUMNSTableAdapter adapterTableName = new COLUMNSTableAdapter();

    DataTable table = adapterTableName.GetData(tableName); // Get the data table.
    foreach (DataRow row in table.Rows) // Loop over the rows.
    {
        foreach (var item in row.ItemArray) // Loop over the items.
        {

            for (int i = 1; i < 30; i++)
            {
                excell_app.createHeaders(1, 1, "" + item + "", i, i, 1, "black", false, 10, "n");
            }

        }
    }
}
3
  • My code will loop 30 times before it gets to the next row in the data table. This is wrong and I am trying to define the count to be a count of the items in the data table Commented Jan 26, 2014 at 10:40
  • You need to calculate amount of cells? Commented Jan 26, 2014 at 10:42
  • 1
    Can you please clarify this question? You have 3 answers, and nobody seems to be able to figure out what the heck you want this code to do. Think Big Picture. Commented Jan 26, 2014 at 12:24

3 Answers 3

3

After your comment you can use another variable:
As @Grundy has suggested in comment - if you use an Array you can use Length of it:

int j = 1;
foreach (DataRow row in table.Rows) // Loop over the rows.
{
    for (int i = 0; i < row.ItemArray.Length; i++, j++)
    {
       excell_app.createHeaders(1, 1, "" + row.ItemArray[i] + "", j, j, 1, "black", false, 10, "n");
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

ItemArray is array so it does not have Count property, use Length instead
All i needs to do is increment each time there is a new item. Once there are no more items then i must stop incrementing.
@Grundy Thanks and right, I hope OP's ItemArray is an array :)
error: Error 1 foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'
@PriceCheaperton It seems that I had more luck in guessing what you were trying to do. Please try to better describe your problems - it will spare your time and others efforts.
3

My code will loop 30 times before it gets to the next row in the data table. This is wrong and I am trying to define the count to be a count of the items in the data table

Then Why don't you use one for loop like this ?

for(int i = 1; i< row.ItemArray.Length+1; i++)
{
   var item = row.ItemArray[i];
   excell_app.createHeaders(1, 1, "" + item + "", i, i, 1, "black", false, 10, "n");
}

10 Comments

Well no because i doesnt actually even loop
All i needs to do is increment each time there is a new item. Once there are no more items then i must stop incrementing.
@PriceCheaperton so you want i to be the total item count in the final?
i is just an incrementing value which references the Excel column ranges. eg, [1,1] and in my code its, i,i. So everytime i get a new item i should have a new incremented i. So if the total items in my datatable is 3. I expect the last incremented i to be 3
@PriceCheaperton hmm,so i should be the column count of the current row?
|
2

Edit:

All i needs to do is increment each time there is a new item. Once there are no more items then i must stop incrementing.

in that case i need to write something which stops it going back to zero for a new row..

int i = 0;
foreach (DataRow row in table.Rows) // Loop over the rows.
{
    foreach (var item in row.ItemArrai) // Loop over the items.
    {
        i++;
        for (int j = 1; j < 30; j++)
        {
            excell_app.createHeaders(1, 1, "" + item + "", j, j, 1, "black", false, 10, "n");
        }
    }
}
Console.WriteLine(i);   

12 Comments

that doesn't change anything
This will loop 30 times before it gets to the next row in the data table. I am trying to define the count to be a count of the items in the data table.
All i needs to do is increment each time there is a new item. Once there are no more items then i must stop incrementing.
It should not increment 30 times!
|

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.