簡體   English   中英

如何在Windows窗體的網格視圖中綁定圖像?

[英]How to bind Image in Grid View on Windows Forms?

我將圖像綁定到資源文件夾中的GridView 。當我加載該窗體時,圖像將是bind.But當從MDIPARENT窗體調用該窗體時,圖像將不會顯示。我在下面附加圖像和代碼。

在網格中綁定圖像

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

從MDIParent致電Child

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

截圖

從MDI父級加載: 從MDI父級加載頁面的屏幕截圖 直接加載: 直接加載頁面的屏幕截圖

當圖像綁定在表單加載事件處理程序中時,在MDIChild的情況下, DataGridViewImageCell的值在顯示時間表時再次重置為null,並且您看不到任何圖像。 當它不是MDChild則保留單元格值並且您看到圖像。 我不清楚為什么在MDIChild情況下發生重置。

訂閱表單的已Shown事件,並將圖像綁定代碼移動到該事件處理程序。 它適用於普通和MDIchild用例。

在從資源文件夾中檢索圖像時,請嘗試使用項目命名空間。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM