错误:无法将System.type类型转换为system.drawing.Image

时间:2011-08-17 15:46:34

标签: c# .net winforms image datagridview

我有两种形式:一种是equipmentfinder,另一种是productdescription。我有productname形式的productimageequipmentfinder列数据网格视图。

当我点击其中一个列(即)productimage列时,它将转到另一个工作正常的页面。

我正在制作WinForms应用程序。

但我想在productimage列中以equipmentfinder形式在另一个图片框中的datagridview中显示所选的产品图片。

所以我这样做了:

 private void productGridview_Cellclick(object sender, DataGridViewCellEventArgs e)
 {
    if (e.ColumnIndex == productgridview.Columns["productimage"].Index)
    {
        ProductDescriptionForm pf = new ProductDescriptionForm();
        pf.ShowDialog(this);
    }
 }

并以productdescription形式我这样做了:

 private void ProductDescriptionForm_Load(object sender, EventArgs e)
 {
    EquipmentFinder eqipu = new EquipmentFinder();
    this.imagepicture.Image = (Image)eqipu.productgridview.SelectedCells.GetType();
 }

但我在这一行收到错误:

this.imagepicture.Image =(Image)eqipu.productgridview.SelectedCells.GetType();
  

错误:无法将类型system.type转换为system.drawing.image

2 个答案:

答案 0 :(得分:1)

有几件事。首先,使用GetType()获取对象类型,而不是对象本身,因此您需要删除GetType()。其次,看起来SelectedCells是一个集合,因此您需要指定单元格索引。

//replace zero index with index of the cell that contains the image
this.imagepicture.Image = (Image)eqipu.productgridview.SelectedCells[0];

答案 1 :(得分:0)

你在尝试在这里检索的是datagridview单元格中存储的对象的类型,它实际上是一个“System.Type”对象而不是“System.Drawing.Image”,铸造它在这里没用。您需要获取单元格的内容,而不是内容的类型。

此代码中还有其他错误!

  • SelectedCells是一组细胞。因此,您不是获取单元格内容的类型,而是获取集合的类型(即DataGridViewSelectedCellCollection)。如果您确定至少选择了一个单元格,则应使用SelectedCells[0]

  • 您想要检索单元格的内容,而不是其类型。使用Value代替GetType()

  • 我不明白你为什么要实例化一个新的EquipmentFinder。这将创建一个空的,永不显示的表单......我建议:创建一个属性来保存ProductDescriptionForm中的图像:

    public Image Picture
    {
        get { return imagepicture.Image; }
        set { imagepicture.Image = value; }
    }
    

然后在显示产品描述表单之前设置此属性:

    private void productGridview_Cellclick(object sender, DataGridViewCellEventArgs e)
    {
      if (e.ColumnIndex == productgridview.Columns["productimage"].Index)
      {
          ProductDescriptionForm pf = new ProductDescriptionForm();
          pf.Picture = (Image)productgridview.SelectedCells[0].Value;
          pf.ShowDialog(this);
      }
   }

并删除您在ProductDescriptionForm_Load中编写的代码。

PS:虽然我希望这段代码有效,但它仍然缺少边界和类型检查。我建议写这样的东西:

    private void productGridview_Cellclick(object sender, DataGridViewCellEventArgs e)
    {
      // Don't handle clicks on the column header.
      if (e.RowIndex == -1) return; 

      // Bad index
      if (e.ColumnIndex != productgridview.Columns["productimage"].Index) return;

      // No selection
      if (productgridview.SelectedCells.Count == 0) return;

      // Make sure we have an image in this cell.
      object selectedValue = productgridview.SelectedCells[0].Value;
      if (selectedValue is Image) 
      {
          // Forms are IDisposable, so use them embedded in a using statement.
          using (ProductDescriptionForm pf = new ProductDescriptionForm())
          {
              pf.Picture = (Image)selectedValue;
              pf.ShowDialog(this);
          }
      }
   }

希望这有帮助。

相关问题