Winform开发 如何为dataGridView 添加CheckBox列,并获取选中行
2015-11-24 09:44
609 查看
//添加CheckBox列
DataGridViewCheckBoxColumn columncb = new DataGridViewCheckBoxColumn();
columncb.HeaderText = "选择";
columncb.Name = "cb_check";
columncb.TrueValue = true;
columncb.FalseValue = false;
//column9.DataPropertyName = "IsScienceNature";
columncb.DataPropertyName = "IsChecked";
dataGridView1.Columns.Add(columncb);
private void SetAllRowChecked()
{
// DataGridCell cel=(sender as DataGridCell).
int count = Convert.ToInt16(this.dataGridView1.Rows.Count.ToString());
for (int i = 0; i < count; i++)
{
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells["cb_check"];
Boolean flag = Convert.ToBoolean(checkCell.Value);
if (flag == false) //查找被选择的数据行
{
checkCell.Value = true;
}
continue;
}
}
/// <summary>
/// 表格单元点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex==0&&e.RowIndex!=-1)
{
//获取控件的值
DataGridViewCheckBoxCell checkCell=(DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells["cb_check"];
Boolean flag = Convert.ToBoolean(checkCell.Value);
if (flag==true)
{
checkCell.Value = false;
}
else
{
checkCell.Value = true;
}
}
}
DataGridViewCheckBoxColumn columncb = new DataGridViewCheckBoxColumn();
columncb.HeaderText = "选择";
columncb.Name = "cb_check";
columncb.TrueValue = true;
columncb.FalseValue = false;
//column9.DataPropertyName = "IsScienceNature";
columncb.DataPropertyName = "IsChecked";
dataGridView1.Columns.Add(columncb);
private void SetAllRowChecked()
{
// DataGridCell cel=(sender as DataGridCell).
int count = Convert.ToInt16(this.dataGridView1.Rows.Count.ToString());
for (int i = 0; i < count; i++)
{
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells["cb_check"];
Boolean flag = Convert.ToBoolean(checkCell.Value);
if (flag == false) //查找被选择的数据行
{
checkCell.Value = true;
}
continue;
}
}
/// <summary>
/// 表格单元点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex==0&&e.RowIndex!=-1)
{
//获取控件的值
DataGridViewCheckBoxCell checkCell=(DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells["cb_check"];
Boolean flag = Convert.ToBoolean(checkCell.Value);
if (flag==true)
{
checkCell.Value = false;
}
else
{
checkCell.Value = true;
}
}
}
相关文章推荐
- finally和return 执行顺序
- while和if的区别
- 【support包学习】DrawerLayout的使用
- C# 位操作
- java常用 api
- java正则表达式抽取html数据
- 使用glide 时候在listview 或者 grideview 中图片自动拉伸问题
- iOS 笔记-关于用户交互的那些事
- sql server 查询某个表的所有触发器名称
- 实现icon图标的数字累加
- Codeblocks使用
- 为什么Java中1000==1000为false而100==100为true
- Xamarin.Android之添加.so文件
- Android 内存泄漏调试
- DPM(Defomable Parts Model) 源码分析-训练(三)
- mfc 对话框添加菜单
- 测试utp端口是否开放
- Unity3D之跑酷地图弯曲效果
- PHP函数补完:var_export()
- Struts2和Spring的整合