[英]Finding datagridview cell type in C#?
我試圖找到每個DatagridviewImageCell並將其屬性ImageLayout
設置為DataGridViewImageCellLayout.Zoom
以便該單元格中的圖像將被縮放。 我正在使用此代碼,但收到錯誤: Unable to cast object of type 'System.Windows.Forms.DataGridViewRow' to type 'System.Windows.Forms.DataGridViewImageCell'.
這里:( (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;
您需要使用行對象循環遍歷行,然后使用單元對象循環遍歷單元格。
像這樣的東西:
foreach (DataGridViewRow dr in dataGridView1.Rows) {
foreach (DataGridViewCell dc in dr.Cells) {
if (dc.GetType() == typeof(DataGridViewImageCell)) {
((DataGridViewImageCell)dc).ImageLayout = DataGridViewImageCellLayout.Zoom;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.