jeudi 30 juin 2016

Why my checkbox value not set to true although it is checked?

I have a list of checkbox that represents column names. The user needs to pick which column that he/she wants to be displayed in datagridview. There is also a checkbox in header that when clicked will set that all the checkbox that represents column names will be selected automatically. The problem is at condition if (cell.Value != null). The cell.Value is set to false. So i cannot get the column names into allSelectedColumn. Why is this happening and how can i solve them. Thank you in advance.

    string selectedColumn, allSelectedColumn;
    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        allSelectedColumn = "";
        if (e.RowIndex >= 0)
        {
            int intIndexColumnVal = e.ColumnIndex;
            if (intIndexColumnVal == 2)
            {
                string selectedColumnSingle = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[2].Value);
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    DataGridViewCheckBoxCell cell = row.Cells[0] as DataGridViewCheckBoxCell;
                    if (cell.Value != null)
                    {
                        if (cell.Value == cell.TrueValue)
                        {
                            selectedColumn = Convert.ToString(dataGridView1.Rows[row.Index].Cells[2].Value);
                            allSelectedColumn = allSelectedColumn + selectedColumn + ",";
                            selectedColumn = "";
                        }
                    }
                }

                if (allSelectedColumn == "")
                {
                    allSelectedColumn = selectedColumnSingle;
                }

                Form2 f2 = new Form2(dataTable, allSelectedColumn);
                f2.ShowDialog();
            }
        }
    }

This is the code when the user checked the header checkbox.

    private void checkboxHeader_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < dataGridView1.RowCount; i++)
        {
            dataGridView1[0, i].Value = ((CheckBox)dataGridView1.Controls.Find("checkboxHeader", true)[0]).Checked;
        }
        dataGridView1.EndEdit();
    }




Aucun commentaire:

Enregistrer un commentaire