Два флажка в одном столбце представления сетки данных - PullRequest
0 голосов
/ 07 января 2019

Как добавить два флажка в столбце представления таблицы данных?

1 Ответ

0 голосов
/ 07 января 2019

ОК, это не простая задача. Но можно сделать :) Простое управление пользователем не является решением.

Необходимо создать соответствующий шаблон для ячейки, а затем создать элемент управления (может быть пользовательский элемент управления) с соответствующим интерфейсом.

Сначала вы должны создать новый тип элемента управления. Я думаю, что это может быть простой пользовательский контроль с некоторыми дополнительными вещами:

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

...