Jacque Scott
asked on
C# CheckState of a Checkbox
I dynamically add a datagridview to my form. When a user types in a number and hits 'Enter' the datagridview is created and populated. I also add a checkbox in the column header so the user can toggle between checking and unchecking all of the boxes. My problem comes when the user clicks on the 'Clear' button. I would like the 'Clear' button to clear the whole grid and 'Uncheck' the column header checkbox if it is checked. Here is my problem:
When I step through my code it is already showing that the column header checkbox is 'Unchecked' when it is actually checked. It's like the state of the checkbox never gets changed.
I am not sure what to do.
Any help is appreciated.
- I 'Check' the column Header checkbox
- The very first time I click on the 'Clear' button the column header checkbox is unchecked
- I 'Check the column Header checkbox a 2nd time
- I click on the 'Clear' button a 2nd time and everything BUT the column header checkbox is cleared
When I step through my code it is already showing that the column header checkbox is 'Unchecked' when it is actually checked. It's like the state of the checkbox never gets changed.
I am not sure what to do.
Any help is appreciated.
Can you paste the current code?
ASKER
When the user types in a number in the 'Search' box this is called:
Here is my code to create and populate the datagridview:
Here is my code to clear the screen:
private void txtSearch_KeyUp(object sender, KeyEventArgs e)
{
if (e == null || e.KeyCode == Keys.Enter)
{
//Clear the datasource and datagridview so everything is starting from scratch
if (HeaderCheckBox != null)
{
HeaderCheckBox.CheckState = CheckState.Unchecked;
}
dgBilling.DataSource = null;
dgBilling.Rows.Clear();
dgBilling.Columns.Clear();
dgBilling.ColumnCount = 0;
BindBillGrid(sSCH, sdb);
}
Here is my code to create and populate the datagridview:
private void BindBillGrid(string sSCH, Boolean sdb)
{
DataSet ds = new DataSet();
IsLoadBillGrid = true;
string strSQL = "My sql statement here";
SqlConnection connection = new SqlConnection(Cm.adrl);
SqlDataAdapter dataadapter = new SqlDataAdapter(strSQL, connection);
connection.Open();
dataadapter.Fill(ds);
connection.Close();
//Set AutoGenerateColumns False
dgBilling.AutoGenerateColumns = false;
//Set Columns Count
dgBilling.ColumnCount = 8;
//Add Columns
DataGridViewCheckBoxColumn clsCol2 = new DataGridViewCheckBoxColumn();
clsCol2.HeaderText = "";
clsCol2.ValueType = typeof(System.Boolean);
clsCol2.Width = 55;
clsCol2.Name = "dgcChecked";
clsCol2.DataPropertyName = "Checked";
clsCol2.SortMode = DataGridViewColumnSortMode.Automatic;
clsCol2.FalseValue = false;
clsCol2.TrueValue = true;
dgBilling.Columns.Insert(0, clsCol2);
AddHeaderCheckBox();
HeaderCheckBox.KeyUp += new KeyEventHandler(HeaderCheckBox_KeyUp);
HeaderCheckBox.MouseClick += new MouseEventHandler(HeaderCheckBox_MouseClick);
dgBilling.CellValueChanged += new DataGridViewCellEventHandler(dgBilling_CellValueChanged);
dgBilling.CurrentCellDirtyStateChanged += new EventHandler(dgBilling_CurrentCellDirtyStateChanged);
dgBilling.CellPainting += new DataGridViewCellPaintingEventHandler(dgBilling_CellPainting);
dgBilling.Columns[1].Name = "dgcMatterID";
dgBilling.Columns[1].HeaderText = "Matter ID";
dgBilling.Columns[1].DataPropertyName = "matter_code";
dgBilling.Columns[1].Width = 103;
dgBilling.Columns[1].ReadOnly = true;
dgBilling.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
dgBilling.Columns[2].Name = "dgcDescription";
dgBilling.Columns[2].HeaderText = "Description";
dgBilling.Columns[2].DataPropertyName = "matter_name";
dgBilling.Columns[2].Width = 240;
dgBilling.Columns[2].ReadOnly = true;
dgBilling.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
dgBilling.Columns[3].Name = "dgcTranDate";
dgBilling.Columns[3].HeaderText = "Date";
dgBilling.Columns[3].DataPropertyName = "tran_date";
dgBilling.Columns[3].Width = 65;
dgBilling.Columns[3].ReadOnly = true;
dgBilling.Columns[3].DefaultCellStyle.Format = "MM/dd/yy";
dgBilling.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
dgBilling.Columns[4].Name = "dgcType";
dgBilling.Columns[4].HeaderText = "Type";
dgBilling.Columns[4].DataPropertyName = "Type";
dgBilling.Columns[4].Width = 44;
dgBilling.Columns[4].ReadOnly = true;
dgBilling.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dgBilling.Columns[5].Name = "dgcBillNo";
dgBilling.Columns[5].HeaderText = "Bill#";
dgBilling.Columns[5].DataPropertyName = "bill_num";
dgBilling.Columns[5].Width = 55;
dgBilling.Columns[5].ReadOnly = true;
dgBilling.Columns[5].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
dgBilling.Columns[6].Name = "dgcBillAmt";
dgBilling.Columns[6].HeaderText = "Bill Amt";
dgBilling.Columns[6].DataPropertyName = "BillAmt";
dgBilling.Columns[6].Width = 85;
dgBilling.Columns[6].ReadOnly = true;
dgBilling.Columns[6].DefaultCellStyle.Format = "C";
dgBilling.Columns[6].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
dgBilling.Columns[7].Name = "dgcPmtAmt";
dgBilling.Columns[7].HeaderText = "Pmt Amt";
dgBilling.Columns[7].DataPropertyName = "PmtAmt";
dgBilling.Columns[7].Width = 85;
dgBilling.Columns[7].ReadOnly = true;
dgBilling.Columns[7].DefaultCellStyle.Format = "C";
dgBilling.Columns[7].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
dgBilling.Columns[8].Name = "dgcTotalAR";
dgBilling.Columns[8].HeaderText = "A/R";
dgBilling.Columns[8].DataPropertyName = "TOTAL_AR";
dgBilling.Columns[8].Width = 85;
dgBilling.Columns[8].ReadOnly = true;
dgBilling.Columns[8].DefaultCellStyle.Format = "C";
dgBilling.Columns[8].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
dgBilling.DataSource = ds.Tables[0];
dgBilling.Size = new System.Drawing.Size(840, 417);
tabBilling.Size = new System.Drawing.Size(840, 417);
tabsBillingTime.Size = new System.Drawing.Size(850, 444);
TotalCheckBoxes = 0;
TotalCheckedCheckBoxes = 0;
//Variables for HeaderCheckBox
TotalCheckBoxes = dgBilling.RowCount;
TotalCheckedCheckBoxes = 0;
}
Here is my code to clear the screen:
private void btnClear_Click(object sender, EventArgs e)
{
DialogResult dResult = (DialogResult)MessageBox.Show("Are you sure you want to clear search?", "Clearing Searches - Start Empty Application ", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dResult == DialogResult.Yes)
{
TotalCheckBoxes = 0;
TotalCheckedCheckBoxes = 0;
HeaderCheckBox.CheckState = CheckState.Unchecked;
RowCheckBoxClick((DataGridViewCheckBoxCell)dgBilling[0, 0]);
txtSearch.Text = "";
txtSearch.ForeColor = System.Drawing.Color.Gray;
txtSearch.Text = "Search By Matter ID, Description, or Client Sort";
txtSearch_KeyUp(sender, null);
lblMatterCreditAmt.Text = "$0.00";
lblTrustBalanceAmt.Text = "$0.00";
lblAOL.Text = "";
lblClient.Text = "";
lblReceipts.Visible = false;
dgReceipts.Visible = false;
lblCreditTransfer.Visible = false;
dgCreditTransfers.Visible = false;
}
}
What does the code on your header checkbox that checks all the results look like?
ASKER
private void HeaderCheckBox_MouseClick(object sender, MouseEventArgs e)
{
HeaderCheckBoxClick((CheckBox)sender);
}
private void HeaderCheckBoxClick(CheckBox HCheckBox)
{
IsHeaderCheckBoxClicked = true;
foreach (DataGridViewRow Row in dgBilling.Rows)
((DataGridViewCheckBoxCell)Row.Cells["dgcChecked"]).Value = HCheckBox.Checked;
dgBilling.RefreshEdit();
TotalCheckedCheckBoxes = HCheckBox.Checked ? TotalCheckBoxes : 0;
GetCheckedTotals();
IsHeaderCheckBoxClicked = false;
}
private void RowCheckBoxClick(DataGridViewCheckBoxCell RCheckBox)
{
if (RCheckBox != null)
{
//Modifiy Counter;
if (Convert.ToBoolean(RCheckBox.Value) && TotalCheckedCheckBoxes < TotalCheckBoxes)
TotalCheckedCheckBoxes++;
else if (TotalCheckedCheckBoxes > 0)
TotalCheckedCheckBoxes--;
//Change state of the header CheckBox.
if (TotalCheckedCheckBoxes == 0 && TotalCheckBoxes == 0)
HeaderCheckBox.Checked = false;
else if (TotalCheckedCheckBoxes < TotalCheckBoxes)
HeaderCheckBox.Checked = false;
else if (TotalCheckedCheckBoxes == TotalCheckBoxes)
HeaderCheckBox.Checked = true;
GetCheckedTotals();
}
}
ASKER
I got the code for creating and toggling the checkboxes here:
http://www.codeproject.com/Articles/42437/Toggling-the-States-of-all-CheckBoxes-Inside-a-Dat
http://www.codeproject.com/Articles/42437/Toggling-the-States-of-all-CheckBoxes-Inside-a-Dat
Instead of calling RowCheckBoxClick() do you mean to be calling HeaderCheckBoxClick() ? The RowCheckBoxClick() looks like it's meant to run each time a checkbox other than the header is toggled.
Also this seems to be an issue:
Because 2 of these conditions can exist at the same time. Can it not be?
Also this seems to be an issue:
//Change state of the header CheckBox.
if (TotalCheckedCheckBoxes == 0 && TotalCheckBoxes == 0)
HeaderCheckBox.Checked = false;
else if (TotalCheckedCheckBoxes < TotalCheckBoxes)
HeaderCheckBox.Checked = false;
else if (TotalCheckedCheckBoxes == TotalCheckBoxes)
HeaderCheckBox.Checked = true;
Because 2 of these conditions can exist at the same time. Can it not be?
if (TotalCheckedCheckBoxes < TotalCheckBoxes)
HeaderCheckBox.Checked = false;
else if (TotalCheckedCheckBoxes == TotalCheckBoxes)
HeaderCheckBox.Checked = true;
ASKER
Did you mean calling HeaderCheckBoxClick() in btnClear_Click?
If so I changed
RowCheckBoxClick((DataGrid ViewCheckB oxCell)dgB illing[0, 0]);
to
HeaderCheckBoxClick((Check Box)Header CheckBox);
And get the same results. The 2nd time around the header checkbox stays checked.
You are correct when you say the two conditions can be the same but I if the two totals are 0 then I want HeaderCheckBox.Checked = false; Otherwise HeaderCheckBox.Checked = true;
If so I changed
RowCheckBoxClick((DataGrid
to
HeaderCheckBoxClick((Check
And get the same results. The 2nd time around the header checkbox stays checked.
You are correct when you say the two conditions can be the same but I if the two totals are 0 then I want HeaderCheckBox.Checked = false; Otherwise HeaderCheckBox.Checked = true;
ASKER
My ultimate goal is to have a checkbox in the header of the checkbox column. I want the user to have the ability to toggle between checking/unchecking all of the checkboxes with one click.
I found the code here to accomplish this.
http://www.codeproject.com/Articles/42437/Toggling-the-States-of-all-CheckBoxes-Inside-a-Dat
The code works great until I need to clear the grid and the header checkbox and start over with a new number. I just need to make some changes but I am having trouble.
I found the code here to accomplish this.
http://www.codeproject.com/Articles/42437/Toggling-the-States-of-all-CheckBoxes-Inside-a-Dat
The code works great until I need to clear the grid and the header checkbox and start over with a new number. I just need to make some changes but I am having trouble.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am new to C# and datagridviews and I am having trouble taking your example of applying it to my code. For one I use a dataset to populate my grid. Would that matter?
It should not matter because a DataSet contains DataTables (which I convert my list into in order to bind it to the datagridview). The difference is that when I convert my list to a datatable, I add the selector column.
-saige-
-saige-
ASKER
Unfortunately I can't figure out how to convert your code to work with how I load my datagridview.
Thank you anyway.
Thank you anyway.
I tested Saige's code and it works, despite the asker having issues implementing it. Anyone else who researches this would find the same, working answer. I'd recommend giving Saige 500 pts for a good thorough answer.