2011-08-17 42 views
0

我有两种形式:一种是equipmentfinder,另一种是productdescription。我有一个datagridview列productnameproductimageequipmentfinder形式。错误:无法将类型System.type转换为system.drawing.Image

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

我正在制作WinForms应用程序。

但我想显示在productimage列中从datagridview在另一个picturebox在equipmentfinder窗体中的产品图像。

所以对于我做这样的:

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(); 

Error: cannot convert type system.type To system.drawing.image

+3

GetType为您提供单元格的类型,而不是单元格的内容作为类型。您可能想尝试放弃GetType()位,并将SelectedCell投射到图像(或单元内的项目) – tonycoupland

+0

如何做到这一点,你会帮助... –

+0

@tony你会帮助这个.. –

回答

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); 
      } 
     } 
    } 

希望这会有所帮助。

+0

hey Odalet我在这一行发生错误pf.picture =(Image)productgridview.SelectedCells [0] .Value;像这样无法投射'System.Int32'类型的对象来键入'System.Drawing.Image'。 –

+0

这意味着所选单元格不包含图像,而是一个整数。你在哪里点击?在包含图像的单元格上? – odalet

+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]; 
相关问题