C# WinForm 实现DataGridView中DataGridViewCheckBoxCell的变通单一勾选
2009-03-24 12:50
507 查看
默认情况下 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;
}
}
}
某些情况下 我们需要利用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;
}
}
}
相关文章推荐
- C# WinForm 实现DataGridView中DataGridViewCheckBoxCell的变通单一勾选
- 原创 c# 封装的带CheckBox的DataGridViewColumnHeaderCell 源码部分 实现DataGridView列头带CheckBox控件实现全选功能,支持列头带标题
- C# Winform中DataGridView的DataGridViewCheckBoxColumn使用方法
- C# Winform DataGridView 公共分页实现
- 转载:winform的DataGridView中用C#实现按钮列置灰
- Winform DataGridView CheckBoxColumn c# 单选 解决方案
- c#在winform中用DataGridView实现分页效果
- C#实现WinForm下DataGridView控件的拷贝和粘贴
- C# winform datagridview中如何实现鼠标右键点击一行数据出现一个带有删除的菜单,并能执行删除操作?
- WinForm中DataGridView控件通过复选框实现多条记录的删除 c#2005
- 如何在winform的 DataGridView 中捕捉 CheckBox ( DataGridViewCheckBoxCell ) 列的状态改变 .
- C# Winform DataGridView 公共分页实现
- 如何在winform的 DataGridView 中捕捉 CheckBox ( DataGridViewCheckBoxCell ) 列的状态改变
- 关于Winform下DataGridView中实现checkbox全选反选、同步列表项的处理
- C# WinForm中实现CheckBox全选反选功能
- winform的DataGridView中捕捉CheckBox ( DataGridViewCheckBoxCell ) 列的状态改变
- c#在winform中用DataGridView实现分页效果
- winform中DataGridView使用DataGridViewCheckBoxColumn实现RadioBox单选功能
- 如何在winform的 DataGridView 中捕捉 CheckBox ( DataGridViewCheckBoxCell ) 列的状态改变
- C# Winform DataGridView分页功能的实现