【问题标题】:Overridden CellPainting in DataGridView displays content only after cell loses focusDataGridView 中重写的 CellPainting 仅在单元格失去焦点后才显示内容
【发布时间】:2020-12-18 20:00:21
【问题描述】:

我已经覆盖了 WinForms DataGridView 中的 CellPainting 以根据各种因素在单元格中显示特定图像。

为了给你更多细节,我在 CellPainting 中重绘了 DataGricViewCheckBoxColumn 的内容;我想显示一个绿色勾号或红十字,而不是默认的黑色勾号。

绘制我使用的图像:

e.Graphics.DrawImage(image, new PointF(centreX - offsetX, centreY - offsetY));

它工作正常,但我的绿色勾号/红十字会仅在单元格失去焦点后显示。有没有办法让它在我点击后立即显示,就像标准复选框一样?

谢谢

【问题讨论】:

    标签: winforms checkbox datagridview


    【解决方案1】:

    处理CellContentClick 事件以切换当前单元格的值(真/假)。 DataGridViewCheckBoxColumnCellContent 是复选框。

    假设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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-04-14
      • 2014-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-28
      • 2011-04-02
      相关资源
      最近更新 更多