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;
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值