一个数据网格视图列中有两个复选框

时间:2019-01-07 11:59:47

标签: c# winforms datagridviewcolumn

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

1 个答案:

答案 0 :(得分:1)

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

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

首先,您必须创建新的控件类型。我认为这可以是简单的用户控制,还有一些其他功能:

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