1

我正在尝试查找每个 DatagridviewImageCell 并将其属性设置ImageLayoutDataGridViewImageCellLayout.Zoom以便缩放该单元格中的图像。我正在使用此代码,但出现错误:Unable to cast object of type 'System.Windows.Forms.DataGridViewRow' to type 'System.Windows.Forms.DataGridViewImageCell'.here: (DataGridViewImageCell Imgrow in dataGridView1.Rows。这是我正在使用的代码。

                    foreach (DataGridViewImageCell Imgrow in dataGridView1.Rows)
                {                       
                    if (dataGridView1.Rows[a].Cells[1].Value == "Image")
                    {
                        Imgrow.ImageLayout = DataGridViewImageCellLayout.Zoom;
                    }
                }

我如何解决它?此外,该列是一个 texbox 列,但我正在使用它来替换单元格。

int a = 0;
dataGridView1.Rows.Insert(0, 1);
dataGridView1.Rows[a].Cells["Column1"] = new DataGridViewImageCell();
dataGridView1.Rows[a].Cells["Column1"].Value = picturebox1.Image; 
4

1 回答 1

3

您需要使用行对象遍历行,然后使用单元格对象遍历单元格。

像这样的东西:

foreach (DataGridViewRow dr in dataGridView1.Rows) {
  foreach (DataGridViewCell dc in dr.Cells) {
    if (dc.GetType() == typeof(DataGridViewImageCell)) {
      ((DataGridViewImageCell)dc).ImageLayout = DataGridViewImageCellLayout.Zoom;
    }
  }
}
于 2012-07-19T22:53:30.280 回答