13
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DataGridViewのRowsとCellsのforeach

13
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>()) {
	}
}
13
12
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
13
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?