如何在 C# winform 中在数据网格视图特定单元格中显示图标,而不是布尔类型的真或假?



如何在数据网格视图指定单元格中显示图标而不是布尔类型的真假? 我的项目资源中有这两个图像(我不知道它是否是存储它们的最佳位置(。这就像竖起大拇指和竖起大拇指的图像。 提前谢谢你! 这是我正在尝试修复的代码,但当然不起作用

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

最新更新