CheckBox Check status in Datagridview is invalid

Source: Internet
Author: User
Tags first row

1, the problem description, first select the first line, then deselect, and then click the Department Select all, the first line does not tick, the status is unchecked.





2. Analysis Code

The first row is selected, the cell's click event is changed to 1, the first row is deselected, and the cell's click event is changed to a selected state of 0

private void Dgvdept_cellcontentclick (object sender, DataGridViewCellEventArgs e)
        {
            if (e.rowindex >= 0 && E.columnindex = = 1)
            {
                if (This.dgvdept.rows[e.rowindex]. cells["Colselect"]. value.tostring () = = "1")
                {
                    This.dgvdept.rows[e.rowindex]. cells["Colselect"]. Value = 0;
                }
                else
                {
                    This.dgvdept.rows[e.rowindex]. cells["Colselect"]. Value = 1;}}
          
        }

2, click the Select All button, at this time the first cell does not end its editing state, at this time to perform a full selection, the state of cell 1 becomes 1 before the end of his editing state, the state Change event value is assigned to 0,

private void Btndeptselectall_click (object sender, EventArgs e)

{
Dgvdept.endedit (); You must add the end Edit status code here, remember
if (Btndeptselectall.text = = "Department Select All")
{
Btndeptselectall.text = "All departments are not selected";
Btndeptselectall.image = Global::kjmonitorsystem.properties.resources.selectnull;
for (int i = 0; i < DgvDept.Rows.Count; i++)
{
This.dgvdept.rows[i]. cells["Colselect"]. Value = 1;
}
}
Else
{
Btndeptselectall.text = "All departments selected";
Btndeptselectall.image = Global::kjmonitorsystem.properties.resources.selectall;
for (int i = 0; i < DgvDept.Rows.Count; i++)
{
This.dgvdept.rows[i]. cells["Colselect"]. Value = 0;
}
}
Dgvdept.clearselection ();
Dgvdept.refresh ();
}













Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.