【NCRE】C#实现表格首列CheckBox全选功能
2015-12-18 19:51
465 查看
Hello,everyone, I'm Cindy!
Today,I will share you my little program inNCRE.It's
about creating a table include the checkbox buttons.
The second step,we must create a new form,put adatagridview on the
D层:
B层:
U层:
That's the results:
Today,I will share you my little program inNCRE.It's
about creating a table include the checkbox buttons.
The first step, we should open the Visual studio2012,then create an empty C# project.
The second step,we must create a new form,put adatagridview on the
form,and rename the control as dgv.
The third step,binding the sources by using the C# code.
D层:
#region"查询学生的学号和姓名-韩梦甜-2015-12-16" /// <summary> /// 查询学生的学号和姓名-韩梦甜-2015-12-16 /// </summary> /// <paramname="studentinfo"></param> /// <returns></returns> public DataTable SelectAllStudent() { DataTable dt = new DataTable(); string cmdText = "selectstudentID,studentName from StudentInfoEntity"; dt =sqlhelper.ExecuteQuery(cmdText, CommandType.Text); return dt; } #endregion
B层:
#region" 查出学生的学号和姓名-韩梦甜-2015-12-16" /// <summary> /// 查出学生的学号和姓名-韩梦甜-2015-12-16 /// </summary> /// <paramname="studentinfo"></param> /// <returns></returns> public DataTable SelectAllStudent() { DataTable dt = new DataTable(); dt =studentinfodal.SelectAllStudent(); return dt; } #endregion
U层:
DataTable dt = new DataTable(); StudentInfoEntityBLL studentinfobll = new StudentInfoEntityBLL(); dt = studentinfobll.SelectAllStudent(); dgv.DataSource = dt;
The fourth step, write the C# class namedAddCheckBoxToDataGridView.
public class AddCheckBoxToDataGridView { public static System.Windows.Forms.DataGridView dgv; public static void AddFullSelect() { if (dgv.Rows.Count < 1) { return; } System.Windows.Forms.CheckBox ckBox = new System.Windows.Forms.CheckBox(); ckBox.Text = "全选"; ckBox.Checked = false; System.Drawing.Rectangle rect = dgv.GetCellDisplayRectangle(0, -1, true); ckBox.Size = new System.Drawing.Size(dgv.Columns[0].Width, 18); ckBox.Location = rect.Location; ckBox.CheckedChanged += new EventHandler(ckBox_CheckedChanged); dgv.Controls.Add(ckBox); } static void ckBox_CheckedChanged(object sender, EventArgs e) { for (int i = 0; i < dgv.Rows.Count; i++) { dgv.Rows[i].Cells[0].Value = ((System.Windows.Forms.CheckBox)sender).Checked; } dgv.EndEdit(); } }
Finally,in the form load event, we should call the function named AddFullSelect.
AddCheckBoxToDataGridView.dgv = dgv; AddCheckBoxToDataGridView.AddFullSelect();
That's the results:
Summary
I'm so exited to write my blog by using english.It's so cool.And I wana write more english blogs.相关文章推荐
- C# — WinForm TreeView篇
- C# — WinForm DataGridView篇
- Web窗体开发
- 关于C#结构体和byte[]之间的转换
- C# 尝试读取或写入受保护的内存,这通常指示其他内存已损坏。错误类型为:System.AccessViolationException。
- C#随机函数使用方法
- c# 中的 for vs foreach
- C#中常用的ToString()方法总结
- C#获取Honeywell voyager 1400g扫码后的数据
- C#当中的多线程_任务并行库(中)
- C#聊天,C#简易聊天室
- c#中的多线程异常
- C#之值类型与引用类型
- C# 中自定义Attribute值的获取与优化
- C#验证字符串是否是数字,是否包括中文,是否是邮箱格式,是否是电话格式
- C#文件上传头文件
- NET 2.0(C#)调用ffmpeg处理视频的方法(转载)
- C#遍历文件夹下所有文件
- 分分钟用上C#中的委托和事件
- C#多线程那点事——信号量(Semaphore)