Hello,everyone, I'm Cindy!
Today,I will share you my little program inNCRE.It's about creating a table include the checkbox buttons.
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;
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(); } }
AddCheckBoxToDataGridView.dgv = dgv; AddCheckBoxToDataGridView.AddFullSelect();
That's the results: