如何在datagridview的同一单元格中添加复选框和标签?

5

大家好,我需要在同一单元格中添加复选框和标签。我不知道如何做到这一点。我可以将DataGridViewColumn设置为复选框,但这样只会显示复选框,没有地方显示标签。有人能帮帮我吗?谢谢。


您可以使用模板列。同时放置两个控件。 - Thit Lwin Oo
我需要在WinForms中实现这个功能。它是否可用? - Prakash Kunwar
1个回答

5
我不认为使用开箱即用的CheckBoxCell可以实现此功能。
以下是一个实现方式,它继承了复选框列并进行了一些自定义绘制以绘制标签。可以通过各种方式进行扩展,例如为标签文本提供绑定(当前需要直接设置标签文本)。我的代码大量借鉴自这个论坛帖子
using System;
using System.Windows.Forms;
using System.Drawing;

public class MyDGVCheckBoxColumn : DataGridViewCheckBoxColumn
{
    private string label;

    public string Label
    {
        get
        {
            return label;
        }
        set
        {
            label = value;
        }
    }

    public override DataGridViewCell CellTemplate
    {
        get
        {
            return new MyDGVCheckBoxCell();
        }
    }
}

public class MyDGVCheckBoxCell : DataGridViewCheckBoxCell
{
    private string label;

    public string Label
    {
        get
        {
            return label;
        }
        set
        {
            label = value;
        }

    }

    protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
    {

        // the base Paint implementation paints the check box
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

        // Get the check box bounds: they are the content bounds
        Rectangle contentBounds = this.GetContentBounds(rowIndex);

        // Compute the location where we want to paint the string.
        Point stringLocation = new Point();

        // Compute the Y.
        // NOTE: the current logic does not take into account padding.
        stringLocation.Y = cellBounds.Y + 2;


        // Compute the X.
        // Content bounds are computed relative to the cell bounds
        // - not relative to the DataGridView control.
        stringLocation.X = cellBounds.X + contentBounds.Right + 2;


        // Paint the string.
        if (this.Label == null)
        {
            MyDGVCheckBoxColumn col = (MyDGVCheckBoxColumn)this.OwningColumn;
            this.label = col.Label;
        }

        graphics.DrawString(
        this.Label, Control.DefaultFont, System.Drawing.Brushes.Red, stringLocation);

    }

}

下面是一个示例代码,展示了如何使用:

private void Form1_Load(object sender, EventArgs e)
{
    MyDGVCheckBoxColumn col = new MyDGVCheckBoxColumn();
    col.Label = "hippo";
    col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
    this.dataGridView1.Columns.Add(col);
    this.dataGridView1.RowCount = 5;

    foreach (DataGridViewRow row in this.dataGridView1.Rows)
    {
        if (row.Index % 2 == 0)
        {
            ((MyDGVCheckBoxCell)row.Cells[0]).Label = "kitten";
        }
    }
}

我想问的一件事是如何在 DataGridViewCheckBox 的 checkState 改变时创建事件。 - Prakash Kunwar
1
@PrakashKunwar 你可以使用celldirtystatechanged事件来实现(还有其他方法,但我发现这种方法通常是最好的),看看这个问题https://dev59.com/O1jUa4cB1Zd3GeqPSpfw#6469559 - David Hall

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接