How to bind Image in Grid View on Windows Forms?
Asked Answered
T

2

7

I bind image to GridView from Resource folder.When I load that Form image will be bind.But When call that form from MDIPARENT form Image will not be showed.I attach image and Code below.

Bind Image in Grid

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 Child from MDIParent

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

Screenshots

Loading from MDI parent: screenshot of loading a page from MDI parent Loading directly: screenshot of loading a page directly

Traipse answered 3/12, 2014 at 5:52 Comment(1)
In what method do you have "Bind Image in Grid" code?Chapter
H
3

When the image is bound in the form load event handler, in case of MDIChild the value of the DataGridViewImageCell is again reset to null by the time form is shown and you don't see any image. When its not MDChild then the cell value is retained and you see the image. I am not clear why the reset is happening in case of MDIChild.

Subscribe to the form's Shown event and move your image binding code to that event handler. It works in both normal and MDIchild use cases.

Herniorrhaphy answered 11/12, 2014 at 8:45 Comment(1)
@PraveenS - if it resolved your problem could you mark it as answerHerniorrhaphy
C
3

Try this, Use your project Namespace, when retrieving the image from 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;       
 }
Congest answered 3/12, 2014 at 7:28 Comment(2)
No problem for me when i doing the same thing what you done on either wayCongest
yes.. thats why i mentioned on either case.. No issues on either caseCongest
H
3

When the image is bound in the form load event handler, in case of MDIChild the value of the DataGridViewImageCell is again reset to null by the time form is shown and you don't see any image. When its not MDChild then the cell value is retained and you see the image. I am not clear why the reset is happening in case of MDIChild.

Subscribe to the form's Shown event and move your image binding code to that event handler. It works in both normal and MDIchild use cases.

Herniorrhaphy answered 11/12, 2014 at 8:45 Comment(1)
@PraveenS - if it resolved your problem could you mark it as answerHerniorrhaphy

© 2022 - 2024 — McMap. All rights reserved.