Is it possible to do start iterating from an element other than the first using foreach?

后端 未结 5 1896
花落未央
花落未央 2020-12-05 01:26

I\'m thinking about implementing IEnumerable for my custom collection (a tree) so I can use foreach to traverse my tree. However as far as I know foreach always starts from

5条回答
  •  盖世英雄少女心
    2020-12-05 02:08

    If you want to skip reading Rows in a DataGridView, try this

    foreach (DataGridViewRow row in dataGridView1.Rows.Cast

    If you want to copy contents of one DataGridView to another skipping rows, try this,

    foreach (DataGridViewRow row in dataGridView1.Rows.Cast().Skip(3))
    {
        foreach (DataGridViewCell cell in row.Cells)
        {
            string value = cell.Value.ToString();
            dataGridView2.Rows[i].Cells[j].Value = cell.Value.ToString();
            j++;
        }
        i++;
        j = 0;
    }
    

    this copies the contents from one DataGridView to another skipping 3 rows.

提交回复
热议问题