Checkbox in Datagrid - check in one click
-
Hi All, I am stuck at some point in implementing checkbox column in datagrid. This is how I am adding checkbox column to the DataGridTableStyle. DataGridColumnStyle boolCol = new DataGridBoolColumn(); boolCol.MappingName = "PrimaryFlag"; boolCol.HeaderText = "Primary"; ((DataGridBoolColumn)boolCol).TrueValue = "true"; ((DataGridBoolColumn)boolCol).FalseValue = "false"; ((DataGridBoolColumn)boolCol).ReadOnly = false; boolCol.Width = 40; ts.GridColumnStyles.Add(boolCol); And from database I am receiving "true" or "false" values in the PrimaryFlag column. When I set the datasource of datagrid with the dataset that I receive from database, it correctly sets the checkboxes as checked or unchecked based on "true" or "false" values in the PrimaryFlag column. Now my question is how can I handle the event such that with one click on the checkbox column it should toggle the check value of column. Right now it requires 2-3 clicks to toggle. Any comments are very appreciated. Thanks Ruchi
-
Hi All, I am stuck at some point in implementing checkbox column in datagrid. This is how I am adding checkbox column to the DataGridTableStyle. DataGridColumnStyle boolCol = new DataGridBoolColumn(); boolCol.MappingName = "PrimaryFlag"; boolCol.HeaderText = "Primary"; ((DataGridBoolColumn)boolCol).TrueValue = "true"; ((DataGridBoolColumn)boolCol).FalseValue = "false"; ((DataGridBoolColumn)boolCol).ReadOnly = false; boolCol.Width = 40; ts.GridColumnStyles.Add(boolCol); And from database I am receiving "true" or "false" values in the PrimaryFlag column. When I set the datasource of datagrid with the dataset that I receive from database, it correctly sets the checkboxes as checked or unchecked based on "true" or "false" values in the PrimaryFlag column. Now my question is how can I handle the event such that with one click on the checkbox column it should toggle the check value of column. Right now it requires 2-3 clicks to toggle. Any comments are very appreciated. Thanks Ruchi
First, declare your variable as a
DataGridBoolColumn
instead of aDataGridColumnStyle
. You can then avoid expensive cast operations and can still add it to theDataGridTableStyle.GridColumnStyles
collection property because it's still aDataGridColumnStyle
object (from which it inherits). SetAllowNull
tofalse
(the default) and make sure that none of your rows contains a NULL value for that column. If they do, theDataGridBoolColumn
will automatically draw a tri-state combo box. In most cases with boolean columns in a database it's best to define a default and not allow nulls anyway - it saves you from having to check for null values since - when converting to abool
- value types cannot benull
.Microsoft MVP, Visual C# My Articles