【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:





Summary


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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 20
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

幸运的梦之星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值