![](/img/trans.png)
[英]How to bind data to the columns in C# Windows Forms grid view, which are stored in rows in SQL table
[英]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;
}
CustomerBalance ChildCustBal = new CustomerBalance();
ChildCustBal.MdiParent = this;
ChildCustBal.Show();
從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.