winforms - DataGridView 中重写的 CellPainting 仅在单元格失去焦点后才显示内容
问题描述
我已经覆盖了 WinForms DataGridView 中的 CellPainting,以根据各种因素在单元格中显示特定图像。
为您提供更多详细信息,在 CellPainting 中,我正在重绘 DataGricViewCheckBoxColumn 的内容;我想显示一个绿色勾号或红十字,而不是默认的黑色勾号。
要绘制我使用的图像:
e.Graphics.DrawImage(image, new PointF(centreX - offsetX, centreY - offsetY));
它工作正常,但我的绿色勾号/红十字会仅在单元格失去焦点后显示。有没有办法让它在我点击后立即显示,就像标准复选框一样?
谢谢
解决方案
处理CellContentClick
事件以切换当前单元格的值(真/假)。a的CellContentDataGridViewCheckBoxColumn
是复选框。
假设DataGridViewCheckBoxColumn
是第一列:
private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex >= 0)
{
var s = sender as DataGridView;
var b = s[e.ColumnIndex, e.RowIndex].Value == null
? true
: (bool)s[e.ColumnIndex, e.RowIndex].Value;
s[e.ColumnIndex, e.RowIndex].Value = !b;
}
}
如果您希望通过单击单元格上的任意位置(而不是仅在复选框上)来切换值,请CellMouseClick
改为处理:
private void dgv_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Left && e.ColumnIndex == 0 && e.RowIndex >= 0)
{
var s = sender as DataGridView;
var b = s[e.ColumnIndex, e.RowIndex].Value == null
? true
: (bool)s[e.ColumnIndex, e.RowIndex].Value;
s[e.ColumnIndex, e.RowIndex].Value = !b;
s.NotifyCurrentCellDirty(true);
}
}
无论哪种方式,CellPainting
事件都会触发并在您单击时绘制指定的图像。
CellPainting
例子:
Bitmap bmp1 = Properties.Resources.GreenImage;
Bitmap bmp2 = Properties.Resources.RedImage;
private void dgv_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex >= 0)
{
var s = sender as DataGridView;
e.Paint(e.CellBounds, DataGridViewPaintParts.Background
| DataGridViewPaintParts.Border
| DataGridViewPaintParts.SelectionBackground
| DataGridViewPaintParts.Focus
| DataGridViewPaintParts.ErrorIcon);
if (e.Value != null)
{
var r = new Rectangle((e.CellBounds.Width - 16) / 2 + e.CellBounds.X,
(e.CellBounds.Height - 16) / 2 + e.CellBounds.Y, 16, 16);
var b = (bool)e.Value;
e.Graphics.DrawImage(b ? bmp1 : bmp2, r);
}
e.Handled = true;
}
}
推荐阅读
- python - 从旧式枚举到新式枚举的快速更新的安全性
- regex - 正则表达式 - 检查“行范围”内的字符
- ios - 是否可以在设备启动时启动 iOS 应用程序?
- kubernetes - 如何使用 kubernetes 将数据从一个容器 cp 到另一个容器
- javascript - 从firebase查询单个值,React Native-Error,未定义
- sql - LEFT JOIN 重复行
- laravel - Laravel 嵌套作业
- r - 是否有允许最大化具有非线性约束的线性目标的 R 包?
- selenium - 在 selenium webdriver 中处理按钮标签
- docker - 无法在容器内调用 docker 命令(docker.sock 绑定问题)