简体   繁体   English

Datagridview强制在一列中只选择一个复选框

[英]Datagridview forcing only one checkbox to be selected in a column

How do I force only a single checkbox to be checked in a column of a Datagridview?如何强制在 Datagridview 的列中只选中一个复选框?

 private void grdRegClass_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (grdRegClass.Columns.IndexOf(grdRegClass.Columns["Status"]) == e.ColumnIndex)
        {
            int currentcolumnclicked = e.ColumnIndex;
            int currentrowclicked = e.RowIndex;
            foreach (DataGridViewRow dr in grdRegClass.Rows)
            {
                dr.Cells[currentcolumnclicked].Value = false;
            }
            grdRegClass.CurrentRow.Cells[currentrowclicked].Value = true;  
        }
    }

You will have to subscribe for the CellValueChanged event of the grid and depending on the check state of the current cell, loop the DataGridView and set true/false as Value for the other cells.您必须订阅网格的CellValueChanged事件,并根据当前单元格的检查状态,循环 DataGridView 并将 true/false 设置为其他单元格的值。

void grd_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
      if ((sender as DataGridView).CurrentCell is DataGridViewCheckBoxCell)
      {
           if (Convert.ToBoolean(((sender as DataGridView).CurrentCell as DataGridViewCheckBoxCell).Value))
           {
                   // Maybe have a method which does the
                    //loop and set value except for the current cell
            }
        }
}
    private void dataGridViewProduit_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if ((sender as DataGridView).CurrentCell is DataGridViewCheckBoxCell)
        {
            if (Convert.ToBoolean(((sender as DataGridView).CurrentCell as DataGridViewCheckBoxCell).Value))
            {
                foreach (DataGridViewRow row in (sender as DataGridView).Rows)
                {
                    if (row.Index != (sender as DataGridView).CurrentCell.RowIndex && Convert.ToBoolean(row.Cells[e.ColumnIndex].Value) == true)
                    {
                        row.Cells[e.ColumnIndex].Value = false;
                    }
                }
            }
        }
    }

    private void dataGridViewClient_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        if (this.dataGridViewClient.IsCurrentCellDirty)
        {
            dataGridViewClient.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    }

You can use CellEndEdit event of DGV , as it occurs after when the cell is modified.您可以使用DGVCellEndEdit事件,因为它发生在修改单元格之后。 Kindly read the comments in below code snippet to use the code below:请阅读以下代码片段中的注释以使用以下代码:

private void dgrvUserProfileView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    int CheckedCount = 0;

    //Make sure you have set True Value/ false Value property of check box column to 1/0 or true/false resp.
    //Lets say your column 5th(namely Department) is a checked box column
    if (dgrvUserProfileView.Columns[e.ColumnIndex].Name == "Department")
    {
        for (int i = 0; i <= dgrvUserProfileView.Rows.Count - 1; i++)
        {
            if (Convert.ToBoolean(dgrvUserProfileView.Rows[i].Cells["Department"].Value) == true)
            {
                CheckedCount = CheckedCount + 1;
            }
        }

        if (CheckedCount == 1)
        {
            for (int i = 0; i <= dgrvUserProfileView.Rows.Count - 1; i++)
            {
                if (Convert.ToBoolean(dgrvUserProfileView.Rows[i].Cells["Department"].Value) == true)
                {
                    dgrvUserProfileView.Rows[i].Cells["Department"].ReadOnly = true;
                }
            }
        }
        else
        {
            for (int i = 0; i <= dgrvUserProfileView.Rows.Count - 1; i++)
            {
                dgrvUserProfileView.Rows[i].Cells["Department"].ReadOnly = false;
            }
        }
    }
}

Hope this helps!希望这可以帮助!

private void dgvlist_CellContentClick(object sender, DataGridViewCellEventArgs e)  
    {  
        int currentcolumnclicked = e.ColumnIndex;  
        for (int i = 0; i <= dgvlist.Columns.Count - 1; i++)  
        {  
            if (dgvlist.Columns[i] is DataGridViewCheckBoxColumn)  
            {  
                if (Convert.ToString(dgvlist.CurrentRow.Cells[i].EditedFormattedValue) == "True" && i !=currentcolumnclicked)  
                {  
                    dgvlist.CurrentRow.Cells[i].Value = false;  
                }  
            }  
        }  
    }  
private void dgvCaixa_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((sender as DataGridView).CurrentCell is DataGridViewCheckBoxCell)
            {
                foreach (DataGridViewRow row in dgvCaixa.Rows)
                {
                    if (row.Index != dgvCaixa.CurrentCell.RowIndex && Convert.ToBoolean(row.Cells[e.ColumnIndex].Value) == true)
                    {
                        row.Cells[e.ColumnIndex].Value = false;
                    }
                }
            }
        }

in vb.net:在 vb.net 中:

Private Sub DataGridViewJobsList_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewJobsList.CellValueChanged
    If TypeOf TryCast(sender, DataGridView).CurrentCell Is DataGridViewCheckBoxCell Then
        Dim cell1 As DataGridViewCell
        cell1 = TryCast(sender, DataGridView).CurrentCell
        If cell1.Value = True Then
            For Each row As DataGridViewRow In TryCast(sender, DataGridView).Rows
                If row.Index <> cell1.RowIndex AndAlso row.Cells(e.ColumnIndex).Value = "1" Then
                    row.Cells(e.ColumnIndex).Value = False
                End If
            Next
        End If
    End If
End Sub

Private Sub DataGridViewJobsList_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridViewJobsList.CurrentCellDirtyStateChanged
    If Me.DataGridViewJobsList.IsCurrentCellDirty Then
        DataGridViewJobsList.CommitEdit(DataGridViewDataErrorContexts.Commit)
    End If
End Sub

You can set the VirtualMode setting to TRUE on the DGV to allow only one checkbox.您可以将DGV上的VirtualMode设置设置为TRUE ,以仅允许一个复选框。 and you can set VirtualMode setting to FALSE on the DatagridView to check more than one.并且您可以在DatagridView上将VirtualMode设置设置为FALSE以检查多个。

I think this is the easiest way for the solution.我认为这是解决问题的最简单方法。

Without worrying about the Column and never fail, try this:不用担心 Column 并且永远不会失败,试试这个:

private void DgvIVA_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    DataGridView dg = (DataGridView)sender;
    if (dg.Rows.Count == 0) return;
    if (dg.Rows[e.RowIndex].Cells[e.ColumnIndex].GetType() == typeof(DataGridViewCheckBoxCell))
    {
        int rowSelIndex = e.RowIndex;
        foreach (DataGridViewRow row in dg.Rows)
        {
            if (row.Index != rowSelIndex)
            {
                row.Cells[e.ColumnIndex].Value = false;
            }
        }
    }
}
private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                try
                {
                    string val = dataGridView3.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                    if (val == "False")
                        val = "True";
                    else if (val == "True")
                        val = "False";
                    dataGridView3.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = val;

                for (int i = 0; i < dataGridView3.Rows.Count; i++)
                {
                    string active = "";
                    if (i != e.RowIndex)
                    {
                        if (val == "False")
                        {
                            dataGridView3.Rows[i].Cells[1].Value = "True";
                            active = "Y";
                        }
                        else if (val == "True")
                        {
                            dataGridView3.Rows[i].Cells[1].Value = "False";
                            active = "N";
                        }
                    }
                    else
                    {
                        if (val == "False")
                            active = "N";
                        else
                            active = "Y";
                    }




                }


            }
            catch (Exception ex)
            { }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM