【NCRE】C#实现表格首列CheckBox全选功能

Helloeveryone, I'm Cindy

 

TodayI 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 stepwe must create a new form,put adatagridview on the form,and rename the control as dgv.

 

The third stepbinding 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:


【NCRE】C#实现表格首列CheckBox全选功能_第1张图片



Summary


           I'm so exited to write my blog by using english.It's so cool.And I wana write more english blogs.

你可能感兴趣的:(【NCRE】C#实现表格首列CheckBox全选功能)