1

FOREACHでDataGridViewの次の行の値を取得するにはどうすればよいですか?

foreach (DataGridViewRow row in dataGridViewSelection.Rows)
{
        if ((bool)((DataGridViewCheckBoxCell)row.Cells[3]).Value)
        {
            do
            {
                list.Add(row.Cells[1].Value.ToString());
            } 
            while (row.Cells[2].Value == the next row.Cells[2].Value-->of the next row);

        }              
}

次の行の同じセルの値を取得して、それらを比較できるようにします。ありがとうございました

4

1 に答える 1

2

forの代わりにループを使用する必要がありますが、 DataGridViewRowCollectionが必要な情報を実装しているためforeach、これは簡単です。

    for (int rowNum=0;rowNum<dataGridViewSelection.Rows.Count - 1; ++rowNum)
    {
            DataGridViewRow row = dataGridViewSelection.Rows[rowNum];
            if ((bool)((DataGridViewCheckBoxCell)row.Cells[3]).Value)                    {
                do
                {
                    list.Add(row.Cells[1].Value.ToString());
                } while (row.Cells[2].Value == dataGridViewSelection.Rows[rowNum+1].Cells[2].Value);

            }              
    }

インデックスを使用してインデックスを作成すると、ループ内の他の行に簡単にアクセスできます。

于 2012-05-25T18:29:09.157 に答える