首页 文章

选中/取消选中datagridview上的复选框

提问于
浏览
22

有人可以帮助我为什么它不起作用?我有一个 checkbox ,如果我点击它,这应取消选中datagridview中的所有复选框,这些复选框在包括用户选中复选框之前已经过检查 .

这是代码:

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;
                }
            }
        }

不应选中该复选框 . 应该检查 .

这是添加的列

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

14 回答

  • 0

    所有的铸造都会导致错误,这里我没有尝试过任何工作,所以我摆弄了一下并让它发挥作用 .

    foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[0].Value != null && (bool)row.Cells[0].Value)
                {
                    Console.WriteLine(row.Cells[0].Value);
                }
    
            }
    
  • 0

    看看这个MSDN Forum Posting它建议将Cell的值与Cell.TrueValue进行比较 .

    因此,通过它的示例,您的代码看起来应该是这样的:(这是完全未经测试的)

    编辑:似乎未绑定DataGridViewCheckBox的Cell.TrueValue的默认值为null,您需要在列定义中设置它 .

    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;
            }
        }
    }
    

    这段代码是工作说明在构造函数中设置TrueValue和FalseValue以及检查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();
        }
    }
    
  • 1

    当我看到这篇文章没有真正回答时,我正在制作我自己的Checkbox版本来控制DataGridViewCheckBoxColumn . 要设置DataGridViewCheckBoxCell的已检查状态,请使用:

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

    对于任何试图完成同样事情的人来说,这就是我想出的 .

    这使得两个控件的行为类似于Gmail中的复选框列 . 它保留了鼠标和键盘的功能 .

    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

    这是你可以尝试的另一个例子

    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

    你在这里尝试的代码将翻转复选框的状态(如果为true,则反之亦然),而不管用户选择的复选框,因为这里 foreachselecting 每个 checkbox 并执行操作 .

    为清楚起见,在执行 foreach 操作之前存储 user selected 复选框的 index ,在 foreach 操作之后,通过提及存储的索引并检查它来调用复选框(在您的情况下,将其设为 True - 我认为) .

    这只是逻辑,我确定这是正确的 . 如果可能的话,我会尝试实现一些示例代码 .

    修改你的 foreach 这样的东西:

    //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
    

    上面的函数使所有复选框都为true,包括用户选择的CheckBox . 我想这就是你想要的......

  • 5

    简单的代码就可以了

    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

    尝试下面应该工作的代码

    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;
                }
            }
        }
    }
    
  • 2

    以下代码是完美的

    使用复选框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

    您可以在网格CellClick事件上使用此代码来检查或取消选中单元格复选框:

    private void Grid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                Grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = (Grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null ? true : (!(bool)Grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value));
            }
        }
    
  • 0

    虽然所有其他答案都是正确的,但我会添加另一个对我有用的简单选项:

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

    压缩:

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

    只要 cIndex 指的是 DataGridViewCheckBoxCell 类型的单元格,这将正常工作 . 希望这有助于somone .

  • 1

    你可以试试这段代码:

    DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridView1.CurrentRow.Cells[0];
                    dataGridView1.BeginEdit(true);
                    if (chk.Value == null || (int)chk.Value == 0)
                    {
                        chk.Value = 1;
                    }
                    else
                    {
                        chk.Value = 0;
                    }
                    dataGridView1.EndEdit();
    
  • 25
    // here is a simple way to do so
    
    //irate through the gridview
                foreach (DataGridViewRow row in PifGrid.Rows)
                {
    //store the cell (which is checkbox cell) in an object
                    DataGridViewCheckBoxCell oCell = row.Cells["Check"] as DataGridViewCheckBoxCell;
    
    //check if the checkbox is checked or not
                    bool bChecked = (null != oCell && null != oCell.Value && true == (bool)oCell.Value);
    
    //if its checked then uncheck it other wise check it
                    if (!bChecked)
                    {
                        row.Cells["Check"].Value = true;
    
                    }
                    else
                    {
                        row.Cells["Check"].Value = false;
    
                    }
                }
    
  • 0

    我遇到了同样的问题,即使这里提供的解决方案也没有用 . 复选框根本不会改变,它们的值将保持为空 . 我花了很多年才意识到自己的愚蠢:

    事实证明,在我调用 form1.Show() 之前,我在Form派生类Form1上调用 form1.PopulateDataGridView(my data) . 当我更改顺序时,首先调用 Show() ,然后读取数据并填入复选框,值不会保持为空 .

  • 1

    如果在代码中创建单元格,则下面的代码允许用户取消/检查DataGridView中的复选框

    private void gvData_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)gvData.Rows[e.RowIndex].Cells[0];
    
        if (chk.Value == chk.TrueValue)
        {
            gvData.Rows[e.RowIndex].Cells[0].Value = chk.FalseValue;
        }
        else
        {
            gvData.Rows[e.RowIndex].Cells[0].Value = chk.TrueValue;
        }
    
    }
    

相关问题