在C#中查找datagridview单元格类型?
我正在尝试查找每个DatagridviewImageCell,并将其属性在C#中查找datagridview单元格类型?,c#,winforms,datagridview,visual-c#-express-2010,C#,Winforms,Datagridview,Visual C# Express 2010,我正在尝试查找每个DatagridviewImageCell,并将其属性ImageLayout设置为DataGridViewImageCellLayout.Zoom以便缩放该单元格中的图像。我正在使用此代码,但遇到一个错误:无法将类型为“System.Windows.Forms.DataGridViewRow”的对象强制转换为类型为“System.Windows.Forms.DataGridViewImageCell”。此处:(DataGridViewImageCell在dataGridView
ImageLayout
设置为DataGridViewImageCellLayout.Zoom
以便缩放该单元格中的图像。我正在使用此代码,但遇到一个错误:无法将类型为“System.Windows.Forms.DataGridViewRow”的对象强制转换为类型为“System.Windows.Forms.DataGridViewImageCell”。
此处:(DataGridViewImageCell在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;
您需要使用行对象在行上循环,然后使用单元格对象在单元格上循环
大概是这样的:
foreach (DataGridViewRow dr in dataGridView1.Rows) {
foreach (DataGridViewCell dc in dr.Cells) {
if (dc.GetType() == typeof(DataGridViewImageCell)) {
((DataGridViewImageCell)dc).ImageLayout = DataGridViewImageCellLayout.Zoom;
}
}
}