winform :DataGridView添加一列checkbox

  #region 添加checkbox列

   public void AddCheckBox()
        {

DataGridViewCheckBoxColumn columncb = new DataGridViewCheckBoxColumn();
                columncb.HeaderText = "选择";
                columncb.Name = "cb_check";
                columncb.TrueValue = true;
                columncb.FalseValue = false;
                columncb.DataPropertyName = "IsChecked";
                UserGridView.Columns.Insert(0,columncb);    //添加的checkbox在第一列
                //UserGridView.Columns.Add(columncb);     //添加的checkbox在最后一列

}
        #endregion

在DataGridView控件的CellMouseClick属性中:点击数据勾选上checkbox

private void UserGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {

//checkbox 勾上
            if ((bool)UserGridView.Rows[e.RowIndex].Cells[0].EditedFormattedValue == true)
            {
                this.UserGridView.Rows[e.RowIndex].Cells[0].Value = false;
            }
            else
            {
                this.UserGridView.Rows[e.RowIndex].Cells[0].Value = true;
            }

}

上一篇:OCR是用来做什么的


下一篇:SpringCloud学习6-如何创建一个服务消费者consumer