How to link image in grid mode in Windows Forms?

I am attaching an image to a GridView from the resource folder. When I load this form image, it will be bind.But When calling this form from the MDIPARENT form MDIPARENT image will not be shown. I attach the image and code below.

Link grid image

 DataGridViewImageColumn ic = new DataGridViewImageColumn(); ic.HeaderText = "Payment"; ic.Image = null; ic.Name = "cImg"; ic.Width = 50; dtGrCustBal.Columns.Add(ic); foreach (DataGridViewRow row in dtGrCustBal.Rows) { DataGridViewImageCell cell = row.Cells[10] as DataGridViewImageCell; cell.Value = Properties.Resources.icon_payment_cash_small; } 

Call a child from MDIParent

  CustomerBalance ChildCustBal = new CustomerBalance(); ChildCustBal.MdiParent = this; ChildCustBal.Show(); 

Screenshots

Download from MDI Parent: screenshot of loading a page from MDI parent Direct download: screenshot of loading a page directly

+6
source share
2 answers

When the image is linked in the form load event handler, in the case of MDIChild , the DataGridViewImageCell value is reset again to null in the time form, and you do not see any image. If it is not MDChild , then the cell value is saved, and you see the image. It is not clear why reset occurs in the case of MDIChild .

Subscribe to an event of the Shown form and move the image binding code to this event handler. It works in both regular and MDIChild use cases.

+3
source

Try this, use the project namespace when extracting the image from the resource folder.

 DataGridViewImageColumn ic = new DataGridViewImageColumn(); ic.HeaderText = "Payment"; ic.Image = null; ic.Name = "cImg"; ic.Width = 50; dtGrCustBal.Columns.Add(ic); foreach (DataGridViewRow row in dtGrCustBal.Rows) { DataGridViewImageCell cell = row.Cells[10] as DataGridViewImageCell; cell.Value ="your namespace".Properties.Resources.icon_payment_cash_small; } 
+2
source

Source: https://habr.com/ru/post/979027/


All Articles