簡體   English   中英

在 C# winform 中,如何在 datagridview 特定單元格中顯示圖標而不是布爾類型的 True 或 false?

[英]How can I display icon in datagridview specific cell instead of True or false of boolean type, in C# winform?

如何在 datagridview 特定單元格中顯示圖標而不是布爾類型的 True 或 false? 我的項目資源中有這兩個圖像(我不知道它是否是存儲它們的最佳位置)。 這就像豎起大拇指和豎起大拇指的圖像。 先感謝您! 這是我的代碼我正在嘗試修復但offcourse不起作用

var result = (from u in db.Analys
              join d in db.Department on u.deptId equals d.deptId 
              select new
             {
               AnalysId = u.Id
               Department = d.DepartmenName,                                           
               Accept= u.accept == true ? Resources.thumbsUp : Resources.thumbsDown   

           }).ToList();
           if (result != null)
           {
             daraGridViewResult.DataSource = null;
             daraGridViewResult.DataSource = result;

           }

在網格的 CellPainting 事件上,您可以添加如下代碼:

 e.Paint(e.CellBounds, DataGridViewPaintParts.All);

 var w = Properties.Resources.yes.Width;
 var h = Properties.Resources.yes.Height;
 var x = e.CellBounds.Left + (e.CellBounds.Width - w) / 2;
 var y = e.CellBounds.Top + (e.CellBounds.Height - h) / 2;

 e.Graphics.DrawImage(image, new Rectangle(x, y, w, h));

其中“圖像”是您要顯示的圖像,而不是“真”或“假”。

請記住,此代碼將針對數據網格中的每個單元格執行。 您需要控制這僅適用於布爾列上的單元格。

編輯:

 if (e.ColumnIndex == yourGrid.Columns["Accept"].Index)
 {
      e.Paint(e.CellBounds, DataGridViewPaintParts.All);

      var w = Properties.Resources.yes.Width;
      var h = Properties.Resources.yes.Height;
      var x = e.CellBounds.Left + (e.CellBounds.Width - w) / 2;
      var y = e.CellBounds.Top + (e.CellBounds.Height - h) / 2;

      e.Graphics.DrawImage(image, new Rectangle(x, y, w, h));
 } //if

暫無
暫無

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

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