简体   繁体   中英

How to unmark header checkbox on dataGridView after refreshing the data c#?

Header check box on selection select all the check boxes in the checkbox column. After selecting the header checkbox, when data refreshes, it is still displayed as marked on it. I have the following code when clicking on Ok button, it loads data in the datagridview with header check box and all the check boxes in each rows. Now, select the header checkbox that selects all the check boxes in all the rows. Then, again clicking on Ok button, it reloads the data but the check mark on the header check box is still displaying instead of not checked.

        private void buttonOk_Click(object sender, EventArgs e)
    {
        LoadDataGridView();

        CheckBox ckBoxHeader = new CheckBox();

        //Get the column header cell bounds
        Rectangle rect = datagridview1.GetCellDisplayRectangle(0, -1, true);
        rect.Y = 3;

        //align header check box in the middle center at rect.X = 82
        rect.X = (rect.Location.X + (rect.Width / 2)) - 10 ;

        ckBoxHeader.Size = new Size(18, 18);
        //Change the location of the CheckBox to make it stay on the header
        ckBoxHeader.Location = rect.Location;
        datagridview1.Columns[0].Frozen = true;

        ckBoxHeader.CheckedChanged += ckBoxHeader_CheckedChanged;

        //Add the CheckBox into the DataGridView
        datagridview1.Controls.Add(ckBoxHeader);
    }


   private void ckBoxHeader_CheckedChanged(object sender, EventArgs e)
    {
        for (int j = 0; j <= datagridview1.RowCount - 1; j++)
        {
            datagridview1[0, j].Value = ((CheckBox)sender).Checked;
        }
        datagridview1.EndEdit();

    }

It solved my issue of unselecting the header check box. The above code should be rewritten as follows in order to headerCheckBox.Checked = false; to be working: Load Event Code -

    private void Form1_Load(object sender, EventArgs e)
    {
        addHeaderCheckBox();
        changeDataGridViewColumnReadonly();
        datagridview1.Columns[0].Frozen = true;            
    }

Load datagridview method code -

        private void LoadDataGridView()
    {
        DataTable dt = new DataTable();
        string WhereClause = " WHERE NO LIKE '" + textBoxNumber.Text.ToString().Substring(0,12) + "%' AND (DATE_TIME_START BETWEEN " + General.BuildOraDateString(dateTimePickerStart.Value) + " AND " + General.BuildOraDateString(dateTimePickerEnd.Value) + ") AND STATUS_IND = 'ST' ";

        dt = Unit.GetData(WhereClause);
        this.datagridview1.Refresh();
        this.datagridview1.DataSource = dt;

        headerCheckBox.Checked = false;

        headerCheckBox.MouseClick += new 
        MouseEventHandler(headerCheckBox_MouseClick);

        datagridview1.CellValueChanged +=
      new DataGridViewCellEventHandler(datagridview1_CellValueChanged);

        datagridview1.CurrentCellDirtyStateChanged +=
          new EventHandler(datagridview1_CurrentCellDirtyStateChanged);

        datagridview1.CellPainting +=
          new 
      DataGridViewCellPaintingEventHandler(datagridview1_CellPainting);
    }

Ok button click event-

     private void buttonOk_Click(object sender, EventArgs e)
    {
        LoadDataGridView();
        totalCheckBoxes = dgvBaleDisposition.RowCount;

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM