LoginSignup
12
11

More than 5 years have passed since last update.

DataGridViewのRowsとCellsのforeach

Posted at

以下のようにforeachすると、それぞれのrow/cellがobject型になってしまいます。

foreach(var row in dataGridView1.Rows){} //rowはobject型
foreach(var cell in this.dataGridView1.Rows[1].Cells) {} //cellはobject型

そこで、Cast<>()メソッドを使用して以下のようにすれば、うまくいく。

foreach(var row in dataGridView1.Rows.Cast<DataGridViewRow>()) {
    foreach(var cell in row.Cells.Cast<DataGridViewCell>()) {
    }
}
12
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
11