首页 > 解决方案 > 一个数据网格视图列中的两个复选框

问题描述

如何在数据网格视图列中添加两个复选框?

标签: c#winformsdatagridviewcolumn

解决方案


好吧,这不是简单的任务。但可以做到:) 简单的用户控制不是解决方案。

您必须为单元格创建适当的模板,然后创建具有适当界面的控件(可能是用户控件)。

首先,您必须创建新的控件类型。我认为它可以是简单的用户控件和一些额外的东西:

class CheckBoxesState
{
    public bool Ch1Checked {get;set;}
    public bool Ch2Checked {get;set;}
}

class CheckBoxesControl: UserControl, IDataGridViewEditingControl
{
    DataGridView dataGridView;
    private bool valueChanged = false;
    int rowIndex;
    CheckBoxesState state;

    // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
// property.
    public object EditingControlFormattedValue
    {
        get { return state; }  
        set
        {
            if(value is CheckBoxesState)
            {
                state = value;
                //change checkboxes state in you user control 
            }
        }
    }

    // Implements the 
    // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
    public object GetEditingControlFormattedValue(
    DataGridViewDataErrorContexts context)
    {
        return EditingControlFormattedValue;
    } 

    // Implements the 
    // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
    public void ApplyCellStyleToEditingControl(
        DataGridViewCellStyle dataGridViewCellStyle)
    {
        this.Font = dataGridViewCellStyle.Font;
    }

    // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
    // property.
    public int EditingControlRowIndex
    {
        get
        {
            return rowIndex;
        }
        set
        {
            rowIndex = value;
        }
    }

    // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
    // method.
    public bool EditingControlWantsInputKey(
        Keys key, bool dataGridViewWantsInputKey)
    {
        return !dataGridViewWantsInputKey;
    }

    // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
    // method.
    public void PrepareEditingControlForEdit(bool selectAll)
    {
        // No preparation needs to be done.
    }

    // Implements the IDataGridViewEditingControl
    // .RepositionEditingControlOnValueChange property.
    public bool RepositionEditingControlOnValueChange
    {
        get
        {
            return false;
        }
    }

    // Implements the IDataGridViewEditingControl
    // .EditingControlDataGridView property.
    public DataGridView EditingControlDataGridView
    {
        get
        {
            return dataGridView;
        }
        set
        {
            dataGridView = value;
        }
    }

    // Implements the IDataGridViewEditingControl
    // .EditingControlValueChanged property.
    public bool EditingControlValueChanged
    {
        get
        {
            return valueChanged;
        }
        set
        {
            valueChanged = value;
        }
    }

    // Implements the IDataGridViewEditingControl
    // .EditingPanelCursor property.
    public Cursor EditingPanelCursor
    {
        get
        {
            return base.Cursor;
        }
    }

    protected override void OnValueChanged(EventArgs eventargs)
    {
        // Notify the DataGridView that the contents of the cell
        // have changed.
        valueChanged = true;
        this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
        base.OnValueChanged(eventargs);
    }
}    

接下来,您必须创建新的单元格类型。

public class CheckBoxesCell : DataGridViewCell
{

    public CheckBoxesCell()
        : base()
    {

    }

    public override Type EditType
    {
        get
        {
        // Return the type of the editing control that cell uses.
        return typeof(CheckBoxesControl);
        }
    }

    public override Type ValueType
    {
        get
        {
            // Return the type of the value that Cell contains.

            return typeof(CheckBoxesState);
        }
    }

    public override object DefaultNewRowValue
    {
        get
        {
            // Use the current date and time as the default value.

            return new CheckBoxesState();
        }
    }
}

您应该创建的下一件事是数据网格的新列类型:

public class CheckBoxesColumn : DataGridViewColumn
{
    public CheckBoxesColumn() : base(new CheckBoxesCell())
    {
    }

    public override DataGridViewCell CellTemplate
    {
        get
        {
            return base.CellTemplate;
        }
        set
        {
        // Ensure that the cell used for the template is a CheckBoxesCell.
            if (value != null && 
                !value.GetType().IsAssignableFrom(typeof(CheckBoxesCell)))
            {
                throw new InvalidCastException("Must be a CheckBoxesCell");
            }
            base.CellTemplate = value;
        }
    }
}

这应该就是全部了。现在您只需创建 CheckBoxesColumn 并将其添加到您的数据网格中。

一切都显示在这里:https ://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-host-controls-in-windows-forms-datagridview-cells


推荐阅读