开发者

ERROR: Cannot convert type System.type to system.drawing.Image

开发者 https://www.devze.com 2023-03-28 14:18 出处:网络
I have two forms: one is equipmentfinder and the other is productdescription. I have a datagridview with columns productname and productimage in equipmentfinder form.

I have two forms: one is equipmentfinder and the other is productdescription. I have a datagridview with columns productname and productimage in equipmentfinder form.

When I click on one of the column (i.e.) productimage column it will go to another page that was working fine.

I am making WinForms application.

But I want to display the selected productimage in productimage column from datagridview in another picturebox in equipmentfinder form.

So for that I have done like this:

 private void productGridview_Cel开发者_开发技巧lclick(object sender, DataGridViewCellEventArgs e)
 {
    if (e.ColumnIndex == productgridview.Columns["productimage"].Index)
    {
        ProductDescriptionForm pf = new ProductDescriptionForm();
        pf.ShowDialog(this);
    }
 }

and in productdescription form I have done like this:

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

but I got an error at this line:

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

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


A couple of things. First, using GetType() gets the object type, not the object itself, so you need to remove the GetType(). Secondly, it looks like SelectedCells is a collection, so you would need to specify the cell index.

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


What you are trying to retrieve here is the type of the object stored in the datagridview cell, which is indeed a "System.Type" object and not a "System.Drawing.Image", casting it is of no use here. You need to get the content of the cell, not the content's type.

There are other errors in this code!

  • SelectedCells is a collection of cells. So you aren't getting the type of a cell's content, but the type of the collection (ie DataGridViewSelectedCellCollection). If you are sure at least one cell is selected, you should use SelectedCells[0].

  • You want to retrieve the content of the cell, not its type. Use Valueinstead of GetType().

  • I don't understand why you instantiate a new EquipmentFinder. This will create an empty and never displayed form... What I suggest: create a property to hold the image in ProductDescriptionForm:

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

Then set this property before displaying the product description form:

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

And remove the code you've written in ProductDescriptionForm_Load.

PS: though I hope this code works, it is still missing boundaries and type checking. I would recommend writing something like this:

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

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消