C# WinForm 实现DataGridView中DataGridViewCheckBoxCell的变通单一勾选

默认情况下 DataGridViewCheckBoxColumn 是可以多选的
某些情况下 我们需要利用CheckBox的可勾选及取消勾选的属性
来提供给用户选择项 同时需要勾选某项后 其他行的就取消勾选
如一组人员中 选择一个组长

相关示例代码如下:


//m_PreRoleID 之前组长的UserID
//strCurrectChooseUserID 当前选择的组长的UserID

//DataGridView绑定事件
private void InitDataGridViewBind()
{
    DataTable dtNew = new DataTable();
    dtNew = GlobalStatic.gs_myWS.MonitorGetUserIDInfo().Tables[0];
    //UserID , UserDesc , RoleName
    DataColumn colisChecked = new DataColumn("isChecked");
    colisChecked.DefaultValue = false;
    dtNew.Columns.Add(colisChecked);

    for (int i = 0; i < dtNew.Rows.Count; i++)
    {
        DataRow dr = dtNew.Rows[i];
        if (dtNew.Rows[i]["UserID"].ToString() == m_PreRoleID)
        {
            dtNew.Rows[i]["IsChecked"] = true;
            strCurrectChooseUserID = m_PreRoleID;
            break;
        }
    }
    this.dataGridView1.AutoGenerateColumns = false;
    this.dataGridView1.DataSource = dtNew;
}

//
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.ColumnIndex != -1 && e.RowIndex != -1)
    {
        if (((DataGridViewTextBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells["RoleName"]).Value.ToString() == "组长")
        {
            if (((DataGridViewTextBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells["UserID"]).Value.ToString() != m_PreRoleID)
            {
                MessageInfoForm.Show("该客户端ID已被其他客户端配置使用 不能再用于本客户端的配置");
            }
            else
            {
                if (e.ColumnIndex != 0)
                {
                    ((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells["ColIsChecked"]).Value = true;//选择当前行的CheckBox 并空置其他的行
                    for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                    {
                        if (i != e.RowIndex)
                        {
                            ((DataGridViewCheckBoxCell)this.dataGridView1.Rows[i].Cells["ColIsChecked"]).Value = false;
                        }
                    }
                    this.dataGridView1.Rows[e.RowIndex].Selected = true;
                    strCurrectChooseUserID = ((DataGridViewTextBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells["UserID"]).Value.ToString();
                }
                else
                {
                    if (((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells["ColIsChecked"]).Value.ToString() == "True")
                    {
                        strCurrectChooseUserID = "";
                        ((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells["ColIsChecked"]).Value = false;
                    }
                    else
                    {
                        strCurrectChooseUserID = ((DataGridViewTextBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells["UserID"]).Value.ToString();
                        for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                        {
                            if (i != e.RowIndex)
                            {
                                ((DataGridViewCheckBoxCell)this.dataGridView1.Rows[i].Cells["ColIsChecked"]).Value = false;
                            }
                        }
                        ((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells["ColIsChecked"]).Value = true;
                    }
                }
            }
        }
        else
        {
            if (e.ColumnIndex != 0)
            {
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    if (i != e.RowIndex)
                    {
                        ((DataGridViewCheckBoxCell)this.dataGridView1.Rows[i].Cells["ColIsChecked"]).Value = false;
                    }
                }
                ((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells["ColIsChecked"]).Value = true;//选择当前行的CheckBox 并空置其他的行
                this.dataGridView1.Rows[e.RowIndex].Selected = true;
                strCurrectChooseUserID = ((DataGridViewTextBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells["UserID"]).Value.ToString();
            }
            else
            {
                if (((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells["ColIsChecked"]).Value.ToString() == "True")
                {
                    strCurrectChooseUserID = "";
                    ((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells["ColIsChecked"]).Value = false;
                }
                else
                {
                    strCurrectChooseUserID = ((DataGridViewTextBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells["UserID"]).Value.ToString();
                    for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                    {
                        if (i != e.RowIndex)
                        {
                            ((DataGridViewCheckBoxCell)this.dataGridView1.Rows[i].Cells["ColIsChecked"]).Value = false;
                        }
                    }
                    ((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells["ColIsChecked"]).Value = true;
                }
            }
        }
    }
}

//上任组长显蓝色
//当前选择的组长显红色
//一般组员默认黑色
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (((DataGridViewTextBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells["RoleName"]).Value.ToString() == "组长")
    {
        if (((DataGridViewTextBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells["UserID"]).Value.ToString() == m_PreRoleID)
        {
            this.dataGridView1.Rows[e.RowIndex].Cells["RoleName"].Style.ForeColor = Color.Blue;
        }
        else
        {
            this.dataGridView1.Rows[e.RowIndex].Cells["RoleName"].Style.ForeColor = Color.Red;
        }
    }
}

原文地址:https://www.cnblogs.com/freeliver54/p/1420390.html