0

WinForms DataGridViewDataGridViewCheckBoxColumnに「すべてチェック」機能を追加することは可能ですか?

次のようになります。

ここに画像の説明を入力してください

ハイライトされたチェックボックスをクリックすると、グリッド内のすべてのチェックボックスがオン/オフになります。

ご覧のとおり、列ヘッダーには文字列値のみを含めることができます。回避策はありますか?

4

2 に答える 2

1

これを試してください:http://tech.chitgoks.com/2008/11/17/c-add-select-all-deselect-all-checkbox-in-column-header-in-datagridview-control/

于 2012-09-28T14:57:29.130 に答える
0

最終的な実装は主にソリューションであり、この記事でSamirによって提案されています。

ただし、グリッドの水平スクロールバーが移動しているときは、チェックボックスの位置を修正する必要があります。したがって、変更が必要なメソッドは次のとおりです。

private void frmSelectAll_Load(object sender, EventArgs e)
{
    AddHeaderCheckBox();

    HeaderCheckBox.KeyUp += new KeyEventHandler(HeaderCheckBox_KeyUp);
    HeaderCheckBox.MouseClick += new MouseEventHandler(HeaderCheckBox_MouseClick);
    dgvSelectAll.CellValueChanged += new DataGridViewCellEventHandler(dgvSelectAll_CellValueChanged);
    dgvSelectAll.CurrentCellDirtyStateChanged += new EventHandler(dgvSelectAll_CurrentCellDirtyStateChanged);
    dgvSelectAll.CellPainting += new DataGridViewCellPaintingEventHandler(dgvSelectAll_CellPainting);

    BindGridView();

    var checkboxHeaderCellRect = dgvSelectAll.GetCellDisplayRectangle(0, -1, false);
    headerCheckboxRightMargin = (checkboxHeaderCellRect.Width - HeaderCheckBox.Width)/2;
}

private int headerCheckboxRightMargin;

private void ResetHeaderCheckBoxLocation(int ColumnIndex, int RowIndex)
{
    //Get the column header cell bounds
    Rectangle oRectangle = this.dgvSelectAll.GetCellDisplayRectangle(ColumnIndex, RowIndex, false);

    Point oPoint = new Point();

    oPoint.X = oRectangle.Location.X + (oRectangle.Width - headerCheckboxRightMargin - HeaderCheckBox.Width);
    oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - HeaderCheckBox.Height) / 2 + 1;

    if (oPoint.X < oRectangle.X)
    {
        HeaderCheckBox.Visible = false;
    }
    else
    {
        HeaderCheckBox.Visible = true;
    }

    //Change the location of the CheckBox to make it stay on the header
    HeaderCheckBox.Location = oPoint;
}
于 2012-09-28T15:53:49.507 に答える