Check/Uncheck a checkbox on datagridview

2020-01-27 06:22发布

Can someone help me why it doesn't work? I have a checkbox and if I click on it, this should uncheck all the checkbox inside the datagridview which were checked before including the user selected checkbox.

Here is the code:

        private void chkItems_CheckedChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in datagridview1.Rows)
            {
                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[1];
                if (chk.Selected == true)
                {
                    chk.Selected = false;
                }
                else
                {
                    chk.Selected = true;
                }
            }
        }

the checkbox should not be selected. it should be checked.

here is the added column

            DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
            CheckBox chk = new CheckBox();
            CheckboxColumn.Width = 20;
            datagridview1.Columns.Add(CheckboxColumn);

15条回答
地球回转人心会变
2楼-- · 2020-01-27 07:10

Here is another example you can try

private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == dataGridView.Columns["Select"].Index)
        {
            dataGridView.EndEdit();

            if ((bool)dataGridView.Rows[e.RowIndex].Cells["Select"].Value)
            {
                //-- checking current select, needs to uncheck any other cells that are checked
                foreach(DataGridViewRow row in dataGridView.Rows)
                {
                    if (row.Index == e.RowIndex)
                    {
                        dataGridView.Rows[row.Index].SetValues(true);
                    }
                    else
                    {
                        dataGridView.Rows[row.Index].SetValues(false);
                    }
                }
            }
        }
    }
查看更多
做自己的国王
3楼-- · 2020-01-27 07:10

I use the CellMouseUp event. I check for the proper column

if (e.ColumnIndex == datagridview.Columns["columncheckbox"].Index)

I set the actual cell to a DataGridViewCheckBoxCell

dgvChkBxCell = datagridview.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;

Then check to see if it's checked using EditingCellFormattedValue

if ((bool)dgvChkBxCell.EditingCellFormattedValue) { }

You will have to check for keyboard entry using the KeyUp event and check the .value property and also check that the CurrentCell's column index matches the checkbox column. The method does not provide e.RowIndex or e.ColumnIndex.

查看更多
冷血范
4楼-- · 2020-01-27 07:11

I had the same problem, and even with the solutions provided here it did not work. The checkboxes would simply not change, their Value would remain null. It took me ages to realize my dumbness:

Turns out, I called the form1.PopulateDataGridView(my data) on the Form derived class Form1 before I called form1.Show(). When I changed up the order, that is to call Show() first, and then read the data and fill in the checkboxes, the value did not stay null.

查看更多
登录 后发表回答