vendredi 16 mars 2018

How do get checkbox column to return boolean value?

I have a checkbox column with a checkbox header. When I click the header checkbox all column checkboxes become checked. However when I click a sum values button it returns the error not set to the instance of an object. I have tried changing the if statement of the sum button I have tried to cast datagridview rows in both header checkbox click events and in the button click event with no joy, can anyone help?

This is the code for the header checkbox click:-

private void HeaderCheckBoxClick(CheckBox HCheckBox) {

        IsHeaderCheckBoxClicked = true;
        foreach (DataGridViewRow Row in dataGridView1.Rows)
            ((DataGridViewCheckBoxCell)Row.Cells[0]).Value = true;

        dataGridView1.RefreshEdit();

       IsHeaderCheckBoxClicked = false;

}

This is the sum button click code:-

private void button2_Click(object sender, EventArgs e) { double sum = 0;

        for (int i = 0; i < dataGridView1.Rows.Count; ++i)
        {

           if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == true)
            {
                sum += double.Parse(dataGridView1.Rows[i].Cells[4].Value.ToString());
            }
        }

        label4.Text = (dataGridView1.Rows.Cast<DataGridViewRow>()
  .Where(r => Convert.ToBoolean(r.Cells[0].Value).Equals(true))
  .Sum(t => Convert.ToInt32(t.Cells[4].Value))).ToString();

}




Aucun commentaire:

Enregistrer un commentaire