2

I can't find a way to get my current index position while looping with a foreach in a sheetData.Elements. (Reading an OpenXML document). I had to make my own cell index (cx) and resetting it to 0 when reading a new row.

I tried something like

foreach (Cell c in r.Elements<Cell>().Select((value, i) => new { i, value })) But obviously it doest work. My current code :

y=0
foreach (Row r in sheetData.Elements<Row>())
{
    dataGridView1.Rows.Add();
    cx = 0;
    foreach (Cell c in r.Elements<Cell>())
    {                    
        dataGridView1.Rows[y].Cells[cx].Value = value;
        cx++;
    }                 
    y++;
}

Is there a way to make a foreach without my own index.

Thanks !

jeffk
  • 21
  • 2
  • Does this answer your question? [How do you get the index of the current iteration of a foreach loop?](https://stackoverflow.com/questions/43021/how-do-you-get-the-index-of-the-current-iteration-of-a-foreach-loop) – galdin Dec 22 '21 at 09:56
  • Does this answer your question? [How can I know a row index while iterating with foreach?](https://stackoverflow.com/questions/1540808/how-can-i-know-a-row-index-while-iterating-with-foreach) – Selim Yildiz Dec 22 '21 at 09:56
  • 2
    `foreach (Cell c in r.Elements().((value, i) => new { i, value }))` cannot work as you're calling a nameless function. You need to use LinQ with `.Select((value, i) => new { i, value })`, as stated in the already (first) linked question. – Chrᴉz remembers Monica Dec 22 '21 at 09:57
  • Yes sorry i just failed my initial writing and cannot edit anymore. I tried with .Select... I get ** Error CS0030 Unable to convert type '(DocumentFormat.OpenXml.Spreadsheet.Cell testvalue, int i)' en 'DocumentFormat.OpenXml.Spreadsheet.Cell'** – jeffk Dec 22 '21 at 10:03
  • 1
    @jeffk When doing that, you will not get a `Cell` but an anonymous object(?) containing a variable `value`, here a `Cell`, and a variable `i`, here the index as `int`. It would be `foreach (var c in r.Elements().Select(...)) { dataGridView1.Rows[y].Cells[cx].Value = c.value; /* index is c.i*/ }` . [Look it up in the hightest voted answer of dup question1](https://stackoverflow.com/a/11437562/9338645) – Chrᴉz remembers Monica Dec 22 '21 at 10:19
  • @ChrᴉzremembersMonica Thanks ! (I m so dumb) The working code : ```foreach (Row r in sheetData.Elements()) { dataGridView1.Rows.Add(); foreach (var c in r.Elements().Select((testvalue, i) => (testvalue, i))) { value = c.testvalue.InnerText; MessageBox.Show(c.i.ToString()); dataGridView1.Rows[y].Cells[c.i].Value = value; } }``` – jeffk Dec 22 '21 at 10:34

1 Answers1

0

Thanks Chrᴉz remembers Monica,

The working code :

foreach (Row r in sheetData.Elements<Row>())
                {
                    dataGridView1.Rows.Add();
                    foreach (var c in r.Elements<Cell>().Select((testvalue, i) => (testvalue, i)))
                    {
                        value = c.testvalue.InnerText;
                        MessageBox.Show(c.i.ToString());  
                        dataGridView1.Rows[y].Cells[c.i].Value = value;
                    }
}```
jeffk
  • 21
  • 2