DataGridViewCheckBoxColumn value not being set?
-
Monday, March 12, 2012 4:21 PM
I have a DataGridView control with a DataGridViewCheckBoxColumn in it. I want to know whether a client set the check box to true (marked) or false (unmarked). I added a _CellContentClick event and I do a this.myDataGridView[0, e.RowIndex].Value.ToString() and always get "false" for the value... Any ideas? Is this the right event to catch this on?
I have tried other events such as CellValueChanged but this only seems to work when the form is loaded. Not on a click like I need to.
Any ideas?
Thanks
All Replies
-
Monday, March 12, 2012 4:40 PM
For checkbox column, you need to set TrueValue/FalseValue to 1/0 or True/False resp. Then you can use below code to check:
if (Convert.ToBoolean(this.myDataGridView[0, e.RowIndex].Value) == true) { //if checked then.... }
Amit Govil | Email
"Weeks of coding can save you hours of planning" -
Monday, March 12, 2012 5:02 PM
Thanks; but did not help. Here is the code for both the column and the Grid. I did not see anything there that would prevent it from not showing any value; but I may be wrong:
this.tblLimitDataBy = new System.Windows.Forms.DataGridView(); this.LimitingMarking = new System.Windows.Forms.DataGridViewCheckBoxColumn(); this.LimitingMarking.FalseValue = 0; this.LimitingMarking.HeaderText = "Limit"; this.LimitingMarking.Name = "LimitingMarking"; this.LimitingMarking.Resizable = System.Windows.Forms.DataGridViewTriState.False; this.LimitingMarking.TrueValue = 1; this.LimitingMarking.Width = 25; this.tblLimitDataBy.AllowUserToAddRows = false; this.tblLimitDataBy.AllowUserToDeleteRows = false; this.tblLimitDataBy.AllowUserToResizeColumns = false; this.tblLimitDataBy.AllowUserToResizeRows = false; this.tblLimitDataBy.BackgroundColor = System.Drawing.Color.White; this.tblLimitDataBy.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None; this.tblLimitDataBy.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None; this.tblLimitDataBy.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.tblLimitDataBy.ColumnHeadersVisible = false; this.tblLimitDataBy.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.LimitingMarking, this.MarkingColor,//OTHER COLUMN NOT RELEVANT TO MY QUESTION this.MarkingName}); //OTHER COLUMN NOT RELEVANT TO MY QUESTION dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Window; dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.ControlText; dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Window; dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.ControlText; dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.False; this.tblLimitDataBy.DefaultCellStyle = dataGridViewCellStyle1; this.tblLimitDataBy.Location = new System.Drawing.Point(6, 93); this.tblLimitDataBy.Name = "tblLimitDataBy"; this.tblLimitDataBy.RowHeadersVisible = false; this.tblLimitDataBy.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; this.tblLimitDataBy.Size = new System.Drawing.Size(435, 105); this.tblLimitDataBy.TabIndex = 3; this.tblLimitDataBy.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.tblLimitDataBy_CellContentClick);
-
Monday, March 12, 2012 5:18 PM
You have already set the TrueValue & FalseValue property to 1,0 resp in your code:
this.LimitingMarking.FalseValue = 0;// FALSE VALUE this.LimitingMarking.HeaderText = "Limit"; this.LimitingMarking.Name = "LimitingMarking"; this.LimitingMarking.Resizable = System.Windows.Forms.DataGridViewTriState.False; this.LimitingMarking.TrueValue = 1;// TRUE VALUEYou just need to use the code i posted above in previous post to check if its checked, you can use the CellEndEdit event.
Amit Govil | Email
"Weeks of coding can save you hours of planning" -
Tuesday, March 13, 2012 7:38 AMModerator
int flag = 0; private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 0) { if (this.dataGridView1.CurrentCell != null) { if (this.dataGridView1.CurrentCell.Value != null) { flag = Convert.ToInt16(this.dataGridView1.CurrentCell.Value); } //else //{ // flag = 0; //} Console.WriteLine("dataGridView1_CellEnter:" + flag); } } } private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e) { flag = 0; } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 0) { Console.WriteLine("dataGridView1_CellClick:" + (flag ^= 1)); } }Since the value changed after you leave the cell, so it will not show you the current value you typed, so the way I only can work out is using a flag like this. And we only need to know the current flag state, do the things we needed.
Mike Zhang[MSFT]
MSDN Community Support | Feedback to us
- Edited by Mike Dos ZhangMicrosoft Contingent Staff, Moderator Tuesday, March 13, 2012 7:38 AM
-
Tuesday, March 13, 2012 9:56 AM
Hi,
When you set datagrid column as CheckBox type, On creation of cell it takes type of DataGridViewCheckBoxCell. so click action will be on underlaying check box control, where you are trying to get the cell value which will be updated after cell leave action fires.
So you should get the DataGridViewCheckBoxCell instance from clicked cell, so that you can see updated value on content click itself
void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { var checkControl = dataGridView1[e.ColumnIndex, e.RowIndex] as DataGridViewCheckBoxCell; //checkControl will be null if column type is not of checkbox, for other type it is null. this is just for demonstartion only MessageBox.Show(checkControl.EditedFormattedValue.ToString()); }Hope this helps you...
If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
- Marked As Answer by lgPerf Tuesday, March 13, 2012 3:51 PM
-
Tuesday, March 13, 2012 10:30 AM
Hello,
This is easy, in CellContentClick event invoke EndEdit of the DataGridView as shown below. When the user changes the current row CheckBox value the demo echos this to the IDE output window. Hope this is what you are after.
Data bound (see below for non-data bound)
Public Class DemoCheckedNow Private Sub DemoCheckedNow_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Using MockedData As New DataTable() MockedData.Columns.AddRange(New DataColumn() _ { New DataColumn("Process", GetType(System.Boolean)), New DataColumn("PartName", GetType(System.String)) } ) MockedData.Rows.Add(New Object() {False, "Part A"}) MockedData.Rows.Add(New Object() {True, "Part 99"}) MockedData.Rows.Add(New Object() {True, "Part B"}) DataGridView1.DataSource = MockedData End Using End Sub Private Sub DataGridView1_CellContentClick( ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _ Handles DataGridView1.CellContentClick If e.ColumnIndex = DataGridView1.Columns("Process").Index Then ' Commit change DataGridView1.EndEdit() ' Show results to the IDE Output window If Convert.ToBoolean(DataGridView1(0, e.RowIndex).Value) Then Console.WriteLine("True") Else Console.WriteLine("False") End If End If End Sub End ClassNon-data bound (columns defined in the IDE)
Public Class DemoCheckedNow Private Sub DemoCheckedNow_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load DataGridView1.Rows.Add(New Object() {False, "Part A"}) DataGridView1.Rows.Add(New Object() {True, "Part 99"}) DataGridView1.Rows.Add(New Object() {True, "Part B"}) End Sub Private Sub DataGridView1_CellContentClick( ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _ Handles DataGridView1.CellContentClick If e.ColumnIndex = DataGridView1.Columns("Process").Index Then ' Commit change DataGridView1.EndEdit() ' Show results to the IDE Output window If Convert.ToBoolean(DataGridView1(0, e.RowIndex).Value) Then Console.WriteLine("True") Else Console.WriteLine("False") End If End If End Sub End ClassKSG
-
Tuesday, March 13, 2012 3:52 PMExcellent, that is it!
Thanks!- Edited by lgPerf Tuesday, March 13, 2012 3:52 PM


