簡體   English   中英

在 C# 中將圖像添加到 DataGridViewImageColumn

[英]Adding image to DataGridViewImageColumn in c#

我正在嘗試通過代碼甚至在設計時將圖像添加到 DataGridViewImageColumn

它總是出現帶有“x”表示沒有圖像的默認 c# 圖像

我試過微軟的這段代碼

    Icon treeIcon = new Icon(this.GetType(), "tree.ico");
DataGridViewImageColumn iconColumn = new DataGridViewImageColumn();
iconColumn.Image = treeIcon.ToBitmap();
iconColumn.Name = "Tree";
iconColumn.HeaderText = "Nice tree";
dataGridView1.Columns.Insert(2, iconColumn);

.但是我在資源中添加了一個.ico - 也嘗試了文件路徑 - 異常結果說在表單類中找不到tree.ico

我用圖像替換了圖標

            ataGridViewImageColumn img = new DataGridViewImageColumn();
            img.ValuesAreIcons = true;
            Image image = Image.FromFile("C:/Users/../Desktop/app_edit.png");
            img.Image = image;
            showprocessed_dataGridView.Columns.Add(img);
            img.HeaderText = "Image";
            img.Name = "img";

這導致 ataGridViewImageColumn 中顯示默認的“無圖像”,這個問題的解決方案是什么? 是圖片格式的問題還是什么!! 任何幫助,將不勝感激 ..

Microsoft 的代碼看起來正在更改列標題中的圖像。 它不會添加包含圖像的行。 為此,您需要向綁定到包含正確圖像數據的DataGridView的數據源添加一條記錄,或者手動創建添加到DataGridViewRowDataGridView

使用 RowDataBound 事件將圖像綁定到網格,

protected void gridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
 if(e.Row.RowType == DataControlRowType.DataRow)
 {
  Image imgGridview = (Image) e.Row.FindControl("imgGrv");
  imgGridview.ImageUrl = "images/test.gif";
 }
}

您必須在編寫的第二個代碼img.ValuesAreIconstrue更改為false 它會起作用,當我使用您的代碼解決此問題時,它發生在我身上。

暫無
暫無

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

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