Hi Linda
Thanks for your reply.
The UI uses a TabContol and on each tab page the first object dropped is a
Split Container set to Horizontal, the top part holding a toobar for a
Navigator and the bottom, a checkBox for select all and immediately below
that the DataGridView. At the tab page level, the first 4 tab pages hold
CheckBoxes and then DataGridViews which are configured identically with
ThreeState set to false and values supplied for TrueValue, FalseValue and
IndeterminateValue in the designer. The fifth tab page contains a TabControl
with 4 tab pages with a further 4 checkboxes for select all DataGridViews.
The interesting bit is that despite the DataGridView being set identically
in the designer, and also programmatically setting the state of the select
all checkboxes and also all checkBoxes in DataGridViewCheckBoxColumns to true
or checked in the Load event of the Page, on displaying the form the
behaviour is inconsistent. On each tab page, the checkBox for select all is
exactly as set programmatically. However for the DatGridViews, only the
topmost and visible DataGridView (i.e the top tabPage) reflects what has been
set in code. The not currently selected and thus not visible tab pages when
displayed do not have the checkBoxes for the rows set at all. Yet the
checkBox for "select all" is correct and when the checkChanged event is fired
from the UI, works just fine.
This is consistent with the behaviour of the values of the
DataGridViewCheckBoxColumn values when inspected in the debugger. The
topmost DataGridView when the form was first displayed is correct and
reflects the settings set in the designer and in code. For all other tab
pages, firstly the checkboxes in the DatagridViewCheckBoxColumns are not
checked even though they should have been and secondly the values when
inspected is null (and shouldn't be, so the ThreeState property is obviously
disregarded) and also throws an exception when cast to the expected boolean.
There must be something that is suppressing the setting of values in the
DataGridvVews for all but the initially presented DataGridView in the topmost
tab page. In all cases the "select all" checkBoxes do what they should.
What beats me is why. More importantly I need to be able to rely on the
state of properties and values that have been set in the designer and in code
being exactly as intended and set or I need to be able to override the what I
consider bizarre behaviour and enforce consistency.
You mention the DataGridViewCellStyle, but when I look in the designer, for
each and every DataGridViewCheckBoxColumn this is set to
DataGridViewCellStyle { NullValue=False, Alignment=MiddleCenter } which is
quite explicit and should enforce boolean values. The actual behaviour does
not conform to this for other than the topmost tabPage and DataGridView. Any
attempt to interpret the values as boolean on all but the topmost
DataGridView and TabPage result in an exception being thrown. That should
not be the case.
Is it possible that the Split Container or TabControl components are
interfering with the DataGridView?
The behaviour you explain for the Load event is what I would expect but is
not what I am getting, hence the problem.
cheers
--
PeterW
"Linda Liu [MSFT]" wrote:
Quote:
Hi Peter,
>
Thank you for your prompt response.
>
Quote:
The DataGridViewCheckBoxColumns ThreeState property is set to False which
I would have thought would have resulted in behaviour the same as for a
normal CheckBox where checked/unchecked is a boolean value.
>
Yes, you're right. When the ThreeState property of the
DataGridViewCheckBoxColumn is set to false, the hosted checkbox editing
control can only have two states, i.e. checked/unchecked. In this case, the
DataGridViewCheckBoxColumn.CellTemplate.ValueType is of type System.Boolean
by default.
>
Quote:
The behaviour is this for the first DataGridView but not for the others.
Yet they are all set up absolutely identically. I do not understand the
inconsistency.
>
Do you mean that only the checkbox column in the first DataGridView
supports two states of values, and the checkbox columns in other
DataGridViews support three states of values? I didn't see it in my test.
>
Quote:
but I would have thought that with ThreeState set to False, nulls would
not be possible and a (bool) cast or accessing a boolean value directly
would be appropriate and setting FalseValue, IndeterminateValue and
TrueValue properteis would be unnecessary.
>
Setting the ThreeState property to false doesn't mean that null value is
not allowed in that DataGridViewCheckBoxColumn.
>
If the NullValue property of the object returned by the DefaultCellStyle
property has a value of false, changing the ThreeState property value to
true automatically sets NullValue to Indeterminate. If NullValue has a
value of Indeterminate, changing the ThreeState property value to false
automatically sets NullValue to false.
>
As I have mentioned above, if the ThreeState property is set to false, the
DataGridViewCheckBoxColumn.CellTemplate.ValueType is of type System.Boolean
by default. If the ThreeState property is set to true, the ValueType is of
type CheckState by default. Of course, we could set the checkbox column's
ValueType to any type we want.
>
Whenever we use DataGridViewCheckBoxColumn, we should set its TrueValue,
FalseValue property and also the IndeterminateValue property if the
ThreeState property is true. The TrueValue, FalseValue and
IndeterminateValue properties are used to save the update in the hosted
checkbox into the cell's value. Note that the values of the TrueValue,
FalseValue and IndeterminateValue properties should be of the checkbox
column's ValueType.
>
Let me illuminate this with a simple example. Say I have a DataGridView
with a DataGridViewCheckBoxColumn in it and the checkbox column's
ThreeState property is set to false. Add the following code in the form's
Load event handler:
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.RowCount = 1;
this.Column1.TrueValue = true;
this.Column1.FalseValue = false;
}
>
Run the application. You should see the checkbox in the DataGridView is
unchecked, because the NullValue property of the object returned by the
DefaultCellStyle property has a value of false. The value of the first cell
in DataGridView is null.
>
Click to select the checkbox in the DataGridView and watch the value of the
first cell. You should see the value becomes true.
>
You may read the following MSDN document for another example of using
DataGidViewCheckBoxColumn:
'DataGridViewCheckBoxColumn.ThreeState Property'
http://msdn2.microsoft.com/en-us/lib...datagridviewch
eckboxcolumn.threestate.aspx
>
Hope it helps.
If you have anything unclear, please feel free to let me know.
>
Sincerely,
Linda Liu
Microsoft Online Community Support
>
>