【问题标题】:ERROR: Cannot convert type System.type to system.drawing.Image错误:无法将类型 System.type 转换为 system.drawing.Image
【发布时间】:2011-08-17 15:46:33
【问题描述】:

我有两种形式:一种是equipmentfinder,另一种是productdescription。我有一个 datagridview 列 productnameproductimageequipmentfinder 表单中。

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

我正在制作 WinForms 应用程序。

但我想在 equipmentfinder 表单的另一个图片框中的 datagridview 中的 productimage 列中显示选定的产品图像。

为此,我这样做了:

 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

【问题讨论】:

  • GetType 为您提供单元格的类型,而不是单元格的内容作为类型。您可能想尝试删除 GetType() 位并将 SelectedCell 转换为图像(或单元格内的项目)
  • 怎么做,请您帮忙...
  • @tony 你能帮忙吗...
  • 抱歉耽搁了,请参阅下面 James 的回复,它更好地解释了我所说的内容!

标签: c# .net winforms image datagridview


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您在此处尝试检索的是存储在 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);
              }
          }
       }
    

    希望这会有所帮助。

    【讨论】:

    • 嘿 Odalet 我在这一行遇到错误 pf.picture= (Image)productgridview.SelectedCells[0].Value;像这样无法将“System.Int32”类型的对象转换为“System.Drawing.Image”类型。
    • 这意味着选择的单元格不包含图像,而是一个整数。你在哪里点击?在包含图像的单元格上?
    • 是否需要在 ProductDescriptionForm 加载事件上设置任何内容
    • 嗯...我通过单击列标题重现了这种行为。如果我单击单元格,一切顺利。您可以测试您是单击列标题还是内容单元格 (e.RowIndex == -1) 我将编辑帖子以包含此测试。
    • 我现在必须退出,但我已经制作了一个小例子来说明我认为您正在努力实现的目标。您可以在此处下载演示此内容的 Visual Studio 2008 解决方案(告诉我您何时收到此内容,以便我将其从我的保管箱帐户中删除):dl.dropbox.com/u/14708294/WindowsFormsApplication1.zip
    猜你喜欢
    • 1970-01-01
    • 2019-05-15
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    相关资源
    最近更新 更多