Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 错误:无法将类型System.type转换为System.drawing.Image_C#_.net_Winforms_Image_Datagridview - Fatal编程技术网

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

C# 错误:无法将类型System.type转换为System.drawing.Image,c#,.net,winforms,image,datagridview,C#,.net,Winforms,Image,Datagridview,我有两种形式:一种是equipmentfinder,另一种是productdescription。我有一个datagridview,在equipmentfinder表单中有productname和productimage列 当我点击其中一列(即productimage列)时,它将转到另一个正常工作的页面 我正在申请WinForms 但是我想在equipmentfinder表单的另一个图片框中的datagridview的productimage列中显示所选的productimage 为此,我做了如

我有两种形式:一种是
equipmentfinder
,另一种是
productdescription
。我有一个datagridview,在
equipmentfinder
表单中有
productname
productimage

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

我正在申请WinForms

但是我想在
equipmentfinder
表单的另一个图片框中的datagridview的
productimage
列中显示所选的productimage

为此,我做了如下工作:

 private void productGridview_Cellclick(object sender, DataGridViewCellEventArgs e)
 {
    if (e.ColumnIndex == productgridview.Columns["productimage"].Index)
    {
        ProductDescriptionForm pf = new ProductDescriptionForm();
        pf.ShowDialog(this);
    }
 }
 private void ProductDescriptionForm_Load(object sender, EventArgs e)
 {
    EquipmentFinder eqipu = new EquipmentFinder();
    this.imagepicture.Image = (Image)eqipu.productgridview.SelectedCells.GetType();
 }
    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);
          }
      }
   }
productdescription
表格中,我这样做:

 private void productGridview_Cellclick(object sender, DataGridViewCellEventArgs e)
 {
    if (e.ColumnIndex == productgridview.Columns["productimage"].Index)
    {
        ProductDescriptionForm pf = new ProductDescriptionForm();
        pf.ShowDialog(this);
    }
 }
 private void ProductDescriptionForm_Load(object sender, EventArgs e)
 {
    EquipmentFinder eqipu = new EquipmentFinder();
    this.imagepicture.Image = (Image)eqipu.productgridview.SelectedCells.GetType();
 }
    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);
          }
      }
   }
但我在这行有个错误:

this.imagepicture.Image =(Image)eqipu.productgridview.SelectedCells.GetType();
错误:无法将类型system.type转换为system.drawing.image


您试图在这里检索的是存储在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\u Load
中编写的代码

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

 private void productGridview_Cellclick(object sender, DataGridViewCellEventArgs e)
 {
    if (e.ColumnIndex == productgridview.Columns["productimage"].Index)
    {
        ProductDescriptionForm pf = new ProductDescriptionForm();
        pf.ShowDialog(this);
    }
 }
 private void ProductDescriptionForm_Load(object sender, EventArgs e)
 {
    EquipmentFinder eqipu = new EquipmentFinder();
    this.imagepicture.Image = (Image)eqipu.productgridview.SelectedCells.GetType();
 }
    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);
          }
      }
   }

希望这能有所帮助。

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

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

GetType为您提供单元格的类型,而不是作为类型的单元格内容。您可能想尝试删除GetType()位,然后将所选单元格强制转换为图像(或单元格中的项目)如何操作请您帮助…@tony请您帮助…抱歉延迟,请参阅下面James的回复,其中更好地解释了我说的话!嘿,Odalet我在这一行pf.picture=(Image)productgridview.SelectedCells[0]有一个错误;类似于此,无法将“System.Int32”类型的对象强制转换为“System.Drawing.Image”类型。这意味着所选单元格不包含图像,而是包含整数。你在哪里点击?在包含图像的单元格上?是否需要在ProductDescriptionForm load eventWell上设置任何。。。我通过单击列标题复制了这种行为。如果我点击单元格,一切都会顺利。你可以测试你是在点击列标题还是在点击内容单元格(e.RowIndex=-1)。我将编辑文章以包含此测试。我现在必须退出,但我已经制作了一个小示例,说明我认为你正在尝试实现的目标。您可以在此处下载演示此功能的Visual Studio 2008解决方案(当您获得此功能时请告诉我,以便我将其从dropbox帐户中删除):