Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何在DataGridView中单击一个复选框时切换所有选定行中的所有复选框_.net_Winforms_Checkbox_Datagridview_Selection - Fatal编程技术网

.net 如何在DataGridView中单击一个复选框时切换所有选定行中的所有复选框

.net 如何在DataGridView中单击一个复选框时切换所有选定行中的所有复选框,.net,winforms,checkbox,datagridview,selection,.net,Winforms,Checkbox,Datagridview,Selection,我花了很多时间才找到解决方案,如何在DataGridView中的所有选定行中的DataGridViewCheckBoxColumn的所有复选框单击时进行切换 我想与其他人分享解决方案(C++/CLI): DataGridViewSelectedRowCollection^previousDataGridView\u SelectedRowCollection; 系统::Void SetRedraw(控件^Control,布尔值) { SendMessage(static_cast(control

我花了很多时间才找到解决方案,如何在DataGridView中的所有选定行中的DataGridViewCheckBoxColumn的所有复选框单击时进行切换

我想与其他人分享解决方案(C++/CLI):

DataGridViewSelectedRowCollection^previousDataGridView\u SelectedRowCollection;
系统::Void SetRedraw(控件^Control,布尔值)
{
SendMessage(static_cast(control->Handle.ToPointer()),WM_SETREDRAW,value,0);
}
系统::作废SuspendDrawing(控件^Control)
{
设置重绘(控制,假);
}
System::Void ResumeDrawing(控件^Control,布尔重绘=true)
{
SetRedraw(控制,真);
如果(重新绘制)
控件->刷新();
}
private:System::Void dataGridView\u CellMouseDown(系统::对象^sender,系统::Windows::窗体::DataGridViewCellMouseEventArgs ^e)
{
如果(e->RowIndex<0 | e->ColumnIndex<0)
返回;
以前的DataGridView\u SelectedRowCollection=
e->ColumnIndex==ColumnCheck->Index&&
dataGridView->SelectedRows->Count>1&&
dataGridView[e->ColumnIndex,e->RowIndex]->GetContentBounds(e->RowIndex)。包含(e->X,e->Y)
?
dataGridView->SelectedRows
:
nullptr;
}
private:System::Void dataGridView_CellContentClick(系统::对象^sender,系统::Windows::窗体::DataGridViewCellEventArgs ^e)
{
如果(e->行索引<0
||e->ColumnIndex<0
||e->ColumnIndex!=ColumnCheck->Index)
返回;
如果(dataGridView->CurrentRow->Selected&&(previousDataGridView_SelectedRowCollection|||(previousDataGridView_SelectedRowCollection=dataGridView->SelectedRows)->计数>1)和&previousDataGridView_SelectedRowCollection->包含(dataGridView->CurrentRow))
{
SuspendDrawing(dataGridView);
尝试
{
DataGridViewCell^currentCell=dataGridView->currentCell;
他当选了;
如果(当前单元格->只读)
currentCell=nullptr;
其他的
{
dataGridView->EndEdit();
isSelected=safe_cast(当前单元格->值);
}
对于每个(先前DataGridView\u SelectedRowCollection中的DataGridViewRow^ DataGridViewRow)
{
DataGridViewCell^DataGridViewCell=dataGridViewRow->Cells[e->ColumnIndex];
if(currentCell&!dataGridViewCell->ReadOnly)
dataGridViewCell->Value=isSelected;
dataGridViewCell->Selected=true;
}
}
最后
{
恢复绘图(dataGridView);
}
}
previousDataGridView_SelectedRowCollection=nullptr;
}

我希望这对某人有帮助

DataGridViewSelectedRowCollection ^previousDataGridView_SelectedRowCollection;

System::Void SetRedraw(Control ^control, Boolean value)
{
    SendMessage(static_cast<HWND>(control->Handle.ToPointer()), WM_SETREDRAW, value, 0);
}

System::Void SuspendDrawing(Control ^control)
{
    SetRedraw(control, false);
}

System::Void ResumeDrawing(Control ^control, Boolean redraw = true)
{
    SetRedraw(control, true);
    if (redraw)
        control->Refresh();
}

private: System::Void dataGridView_CellMouseDown(System::Object^ sender, System::Windows::Forms::DataGridViewCellMouseEventArgs^ e)
{
    if (e->RowIndex < 0 || e->ColumnIndex < 0)
        return;
    previousDataGridView_SelectedRowCollection =
        e->ColumnIndex == ColumnCheck->Index &&
        dataGridView->SelectedRows->Count > 1 &&
        dataGridView[e->ColumnIndex, e->RowIndex]->GetContentBounds(e->RowIndex).Contains(e->X, e->Y)
    ?
        dataGridView->SelectedRows
    :
        nullptr;
}

private: System::Void dataGridView_CellContentClick(System::Object^ sender, System::Windows::Forms::DataGridViewCellEventArgs^ e)
{
    if (e->RowIndex < 0
        || e->ColumnIndex < 0
        || e->ColumnIndex != ColumnCheck->Index)
        return;
    if (dataGridView->CurrentRow->Selected && (previousDataGridView_SelectedRowCollection || (previousDataGridView_SelectedRowCollection = dataGridView->SelectedRows)->Count > 1) && previousDataGridView_SelectedRowCollection->Contains(dataGridView->CurrentRow))
    {
        SuspendDrawing(dataGridView);
        try
        {
            DataGridViewCell ^currentCell = dataGridView->CurrentCell;
            Boolean isSelected;
            if (currentCell->ReadOnly)
                currentCell = nullptr;
            else
            {
                dataGridView->EndEdit();
                isSelected = safe_cast<Boolean>(currentCell->Value);
            }
            for each (DataGridViewRow ^dataGridViewRow in previousDataGridView_SelectedRowCollection)
            {
                DataGridViewCell ^dataGridViewCell = dataGridViewRow->Cells[e->ColumnIndex];
                if (currentCell && !dataGridViewCell->ReadOnly)
                    dataGridViewCell->Value = isSelected;
                dataGridViewCell->Selected = true;
            }
        }
        finally
        {
            ResumeDrawing(dataGridView);
        }
    }
    previousDataGridView_SelectedRowCollection = nullptr;
}