Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox in Datagrid not staying unchecked

Status
Not open for further replies.

bbartlin

Programmer
Jan 8, 2004
35
US
I have a datagrid with a simple dataGridBoolColumn that I set up in the grids DataGridColumnStyle Collection Editor. When I run the app, all the checkboxes are checked as they should be (all the data in the table is set to 1 which is the True value).
When I uncheck a box and move focus to the next record, the box is automatically checked again. I have the false value set to zero. I can update any of the test fields in the row and they do not change on leave of record.

Any ideas? Thanks in advance.

 
It is not clear how you bind the DataGrid and refresh it but I think you should implement handlers for the CurrentCellChanged and Click events.
Also you need to call AcceptChanges() on the DataSet.
Here is an example:
Code:
private System.Windows.Forms.DataGrid m_DataGrid;
private System.Windows.Forms.DataGrid.HitTestInfo m_HitTestInfo;
private bool m_MouseEvent=false;
//..
this.m_DataGrid.CurrentCellChanged += new System.EventHandler(this.m_DataGrid_CurrentCellChanged);
this.m_DataGrid.MouseDown += new System.Windows.Forms.MouseEventHandler(this.m_DataGrid_MouseDown);
this.m_DataGrid.Click += new System.EventHandler(this.m_DataGrid_Click);

private void m_DataGrid_CurrentCellChanged(object sender, System.EventArgs e)
{
	if (m_MouseEvent) 
        	OnCellClick(m_DataGrid.CurrentCell.RowNumber,m_DataGrid.CurrentCell.ColumnNumber);
	m_MouseEvent=false;
}
private void m_DataGrid_Click(object sender, System.EventArgs e)
{
	if (m_MouseEvent && m_HitTestInfo.Type==System.Windows.Forms.DataGrid.HitTestType.Cell)
		OnCellClick(m_HitTestInfo.Row,m_HitTestInfo.Column);
	else m_MouseEvent =false;
}

private void m_DataGrid_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
	m_MouseEvent=true;
	m_HitTestInfo=m_DataGrid.HitTest(e.X,e.Y);
}

private void OnCellClick(int vRow, int vColumn)
{
	m_MouseEvent=false;
	//if (m_HitTestInfo.Row >= m_Rows) return;   

	object vCell = m_DataGrid[vRow,vColumn];
	string vColumnName= "..." ; // extract the column name from the vCell object
	switch (vColumnName)
	{
		case "myCheckColumn":
			vCell=m_DataGrid[vRow,vColumn]=((bool) vCell) ? false : true;
			m_DataSet.Tables[0].AcceptChanges();
			break;
		case "myTargetDate":
				// Get Date & time from a Calendar
				m_DataGrid[vRow,vColumn] = "...";	
			  break;
		default:
			break;
	}
}
-obislavu-
 
I took your advice and added a handler for the CurrentCellChanged event. I step through this and it updates the db when I change the checkbox...but then I refresh and all are checked again. The checked should = 1 and the unchecked should = 0. Everything is checked, no matter the value of the field. Any other suggestions?

private void SubmitDataGrid_CurrentCellChanged(object sender, System.EventArgs e)
{
foreach (DataRow myRow in ds1.Tables["DataTemp"].Rows)
{
if (String.Equals(myRow.RowState.ToString(),"Modified"))
{
dataCommand.CommandText = "UPDATE DataTemp " +
"SET submit = " + myRow["Submit"] + " " +
"WHERE System1EntityType = '" + myRow["System1EntityType"].ToString().Trim() + "' " +
"AND System1EntityKey = '" + myRow["System1EntityKey"].ToString().Trim() + "' " +
"AND System2EntityType = '" + myRow["System2EntityType"].ToString().Trim() + "' " +
"AND System2EntityKey = '" + myRow["System2EntityKey"].ToString().Trim() + "'";

SqlDataReader dataReader110 = dataCommand.ExecuteReader();
dataReader110.Close();
myCommand1 = new SqlDataAdapter("SELECT * FROM DataTemp", dataConnection);
ds1 = new DataSet();
myCommand1.Fill(ds1, "DataTemp");
SubmitDataGrid.SetDataBinding(ds1,"DataTemp");
}
}
}
 
It is not a good ideea to iterate in that handler all records in the DataSet in order to discover which one was modified and then update back the database and get back in a new dataset!
This handler is called when a cell is changed!
In this handler you are able to access the curent cell as object o = myDataGrid[iRow][iColumn];
So, here you modify the cehck box value in the myDataGrid object and call AcceptChanges() on the DataSet!
If you want the data source to be in sunch with the changes made in the DataGrid then you have to decide to do that for each cell or fol the whole row or at another time.
If you do that update using a DataAdapter.Update() then you have to call the AcceptChanges() after Update() succeeds.
-obislavu-
 
I got this to work by using True and False for the values instead of 1 and 0. Not sure why this would be if you are allowed to change the Checked and Unchecked values. But you input was valuable, has taught me more about datagrids. Thanks.
 
bbartlin- I have run into the identical problem as you. I'm populating the datgrid from the DB and the checkbox column is loading from a DB field of the bit type. I am using False as the FalseValue and True as the TrueValue. My update is working however the checkbox will not stay unchecked. If I check it and then click elsewhere it goes back to checked...what did you do to resolve this?
 
I know this sounds really silly, but I even tested it a few times to make sure. The default values are False and True, erase these and actually type them in again (in the DataGridColumnStyle Collection Editor). Then it should recognize them for some odd reason.
 
No doubt it is silly but I believe it!! I tried a number of different things and I finally just rebooted and started a test app from scratch and it finally worked like it should. Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top