Check/Uncheck a checkbox on datagridview

前端 未结 16 574
野性不改
野性不改 2020-11-30 11:19

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 check

相关标签:
16条回答
  • 2020-11-30 11:25

    I was making my own version of a Checkbox to control a DataGridViewCheckBoxColumn when I saw this post wasn't actually answered. To set the checked state of a DataGridViewCheckBoxCell use:

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        dataGridView1.Rows[row.Index].SetValues(true);
    }
    

    For anyone else trying to accomplish the same thing, here is what I came up with.

    This makes the two controls behave like the checkbox column in Gmail. It keeps functionality for both mouse and keyboard.

    using System;
    using System.Windows.Forms;
    
    namespace Check_UnCheck_All
    {
        public partial class Check_UnCheck_All : Form
        {
            public Check_UnCheck_All()
            {
                InitializeComponent();
                dataGridView1.RowCount = 10;
                dataGridView1.AllowUserToAddRows = false;
                this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvApps_CellContentClick);
                this.dataGridView1.CellMouseUp += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.myDataGrid_OnCellMouseUp);
                this.dataGridView1.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.myDataGrid_OnCellValueChanged);
                this.checkBox1.Click += new System.EventHandler(this.checkBox1_Click);
            }
    
            public int chkInt = 0;
            public bool chked = false;
    
            public void myDataGrid_OnCellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == dataGridView1.Rows[0].Index && e.RowIndex != -1)
                {
                    DataGridViewCheckBoxCell chk = dataGridView1.Rows[e.RowIndex].Cells[0] as DataGridViewCheckBoxCell;
    
                    if (Convert.ToBoolean(chk.Value) == true) chkInt++;
                    if (Convert.ToBoolean(chk.Value) == false) chkInt--;
                    if (chkInt < dataGridView1.Rows.Count && chkInt > 0)
                    {
                        checkBox1.CheckState = CheckState.Indeterminate;
                        chked = true;
                    }
                    else if (chkInt == 0)
                    {
                        checkBox1.CheckState = CheckState.Unchecked;
                        chked = false;
                    }
                    else if (chkInt == dataGridView1.Rows.Count)
                    {
                        checkBox1.CheckState = CheckState.Checked;
                        chked = true;
                    }
                }
            }
            public void myDataGrid_OnCellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
            {
                // End of edition on each click on column of checkbox
                if (e.ColumnIndex == dataGridView1.Rows[0].Index && e.RowIndex != -1)
                {
                    dataGridView1.EndEdit();
                }
                dataGridView1.BeginEdit(true);
            }
            public void dgvApps_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                if (dataGridView1.CurrentCell.GetType() == typeof(DataGridViewCheckBoxCell))
                {
                    if (dataGridView1.CurrentCell.IsInEditMode)
                    {
                        if (dataGridView1.IsCurrentCellDirty)
                        {
                            dataGridView1.EndEdit();
                        }
                    }
                    dataGridView1.BeginEdit(true);
                }
            }
            public void checkBox1_Click(object sender, EventArgs e)
            {
                if (chked == true)
                {
                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
                        if (chk.Value == chk.TrueValue)
                        {
                            chk.Value = chk.FalseValue;
                        }
                        else
                        {
                            chk.Value = chk.TrueValue;
                        }
                    }
                    chked = false;
                    chkInt = 0;
                    return;
                }
                if (chked == false)
                {
                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        dataGridView1.Rows[row.Index].SetValues(true);
                    }
                    chked = true;
                    chkInt = dataGridView1.Rows.Count;
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 11:30

    The code you are trying here will flip the states (if true then became false vice versa) of the checkboxes irrespective of the user selected checkbox because here the foreach is selecting each checkbox and performing the operations.

    To make it clear, store the index of the user selected checkbox before performing the foreach operation and after the foreach operation call the checkbox by mentioning the stored index and check it (In your case, make it True -- I think).

    This is just logic and I am damn sure it is correct. I will try to implement some sample code if possible.

    Modify your foreach something like this:

        //Store the index of the selected checkbox here as Integer (you can use e.RowIndex or e.ColumnIndex for it).
        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;
                }
            }
        }
        //write the function for checking(making true) the user selected checkbox by calling the stored Index
    

    The above function makes all the checkboxes true including the user selected CheckBox. I think this is what you want..

    0 讨论(0)
  • 2020-11-30 11:30

    Simple code for you it will work

    private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (dgv.CurrentRow.Cells["ColumnNumber"].Value != null && (bool)dgv.CurrentRow.Cells["ColumnNumber"].Value)
        {
            dgv.CurrentRow.Cells["ColumnNumber"].Value = false;
            dgv.CurrentRow.Cells["ColumnNumber"].Value = null;
        }
        else if (dgv.CurrentRow.Cells["ColumnNumber"].Value == null )
        {
            dgv.CurrentRow.Cells["ColumnNumber"].Value = true;
        }
    }
    
    0 讨论(0)
  • 2020-11-30 11:32

    Below Code is working perfect

    Select / Deselect a check box column on data grid by using checkbox CONTROL

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked == false)
            {
                foreach (DataGridViewRow row in dGV1.Rows)
                {
                    DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
    
                        chk.Value = chk.TrueValue;
                }
           }
           else if(checkBox2.Checked==true)
           {
                foreach (DataGridViewRow row in dGV1.Rows)
                {
                    DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
                    chk.Value = 1;
                    if (row.IsNewRow)
                    {
                        chk.Value = 0;
                    }
                }
            }
        }
    
    0 讨论(0)
  • 2020-11-30 11:35

    While all the other answers are correct, I'll add another simple option which worked for me:

    var r = dataGridView.Rows[rIndex];
    var c = r.Cells[cIndex];
    var value = (bool) c.Value; 
    c.Value = !value;
    

    Compressed:

    var r = dataGridView.Rows[rIndex];
    r.Cells[cIndex].Value = !((bool) r.Cells[cIndex].Value)
    

    As long as cIndex refers to a cell that is of type DataGridViewCheckBoxCell, this will work fine. Hope this helps somone.

    0 讨论(0)
  • 2020-11-30 11:38

    Looking at this MSDN Forum Posting it suggests comparing the Cell's value with Cell.TrueValue.

    So going by its example your code should looks something like this:(this is completely untested)

    Edit: it seems that the Default for Cell.TrueValue for an Unbound DataGridViewCheckBox is null you will need to set it in the Column definition.

    private void chkItems_CheckedChanged(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[1];
            if (chk.Value  == chk.TrueValue)
            {
                chk.Value = chk.FalseValue;
            }
            else
            {
                chk.Value = chk.TrueValue;
            }
        }
    }
    

    This code is working note setting the TrueValue and FalseValue in the Constructor plus also checking for null:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
            CheckboxColumn.TrueValue = true;
            CheckboxColumn.FalseValue = false;
            CheckboxColumn.Width = 100;
            dataGridView1.Columns.Add(CheckboxColumn);
            dataGridView1.Rows.Add(4);
        }
    
        private void chkItems_CheckedChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
                if (chk.Value == chk.FalseValue || chk.Value == null)
                {
                    chk.Value = chk.TrueValue;
                }
                else
                {
                    chk.Value = chk.FalseValue;
                }
    
            }
            dataGridView1.EndEdit();
        }
    }
    
    0 讨论(0)
提交回复
热议问题