Winform中DataGridView的DataGridViewCheckBoxColumn使用方法(选中与选不中)

摘自:http://www.cnblogs.com/blosaa/archive/2013/03/06/2946005.html

给分配个任务,datagridview的checkbox中只能选中一个,参照上面博客,代码如下:

   /// <summary>
        /// 将当前单元格中的更改提交到数据缓存,但不结束编辑模式,及时获得其状态是选中还是未选中    
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView2_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dataGridView2.IsCurrentCellDirty)
            {
                dataGridView2.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }   
        }

        private void dataGridView2_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dataGridView2.Rows.Count > 0)
                {
                    int rowIndex = dataGridView2.CurrentCell.RowIndex;
                    int colIndex = dataGridView2.CurrentCell.ColumnIndex;
                    if (colIndex == 0) //第一列
                    {
                        string _selectValue = dataGridView2.CurrentCell.EditedFormattedValue.ToString();
                        if (_selectValue == "True")
                        {
                            for (int i = 0; i < dataGridView2.Rows.Count; i++)
                            {
                                if (i != rowIndex)
                                {
                                    string otherValue = dataGridView2.Rows[i].Cells[0].EditedFormattedValue.ToString();
                                    if (otherValue == "True")
                                    {
                                        ((DataGridViewCheckBoxCell)dataGridView2.Rows[i].Cells[0]).Value = false;
                                    }
                                }
                            }
                        }
                    }
                    
                }

            }
            catch (Exception ex)
            { }
        }

为datagridview添加两个事件

原文地址:https://www.cnblogs.com/nygfcn1234/p/3532486.html