Microsoft KB Archive/327305

= BUG: Inconsistent behavior when you bind the Visible property of a Windows Form control to a Boolean field =

Article ID: 327305

Article Last Modified on 2/12/2007

-

APPLIES TO


 * Microsoft .NET Framework Class Libraries 1.1
 * Microsoft .NET Framework Class Libraries 1.0
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q327305



SYMPTOMS
When you bind the Visible property of a Windows Form control to a Boolean field, the property setting is inconsistent with the values in the underlying fields.



CAUSE
This behavior occurs because the Visible property is incorrectly updated to the value in the Boolean field.



RESOLUTION
Instead of binding the Visible property directly to the Boolean field, programmatically set the Visible property of the control based on the current value of the Boolean field.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to Reproduce the Behavior
 Start Visual Basic .NET, and then open a new project.

By default, Form1 is created. Add a DataGrid control to the form. Add five TextBox controls to the form. On the View menu, click Code to view the Code window of the form.  Add the following code to the Load event handler of the form: Private Sub Form1_Load(ByVal sender As System.Object, _                          ByVal e As System.EventArgs) _ Handles MyBase.Load

Dim dt As DataTable Dim DataGridTableStyle1 As DataGridTableStyle Dim dgBoolColumn(4) As DataGridBoolColumn Dim drc As DataRowCollection Dim dr As DataRow Dim dc As DataColumn Dim i, j As Integer

'Create a new DataTable object. dt = New DataTable

'Create a new DataGridTableStyle object. DataGridTableStyle1 = New DataGridTableStyle DataGridTableStyle1.DataGrid = Me.DataGrid1 'Add Boolean columns to the TableStyle. For i = 0 To 4 dgBoolColumn(i) = New DataGridBoolColumn With dgBoolColumn(i) .AllowNull = False .TrueValue = True .FalseValue = False .MappingName = &quot;Field&quot; & i + 1 .Width = 75 End With DataGridTableStyle1.GridColumnStyles.Add(dgBoolColumn(i)) Next DataGrid1.TableStyles.Add(DataGridTableStyle1)

'Create 5 columns in the DataTable with a Boolean type. For i = 1 To 5 dc = New DataColumn dc.ColumnName = &quot;Field&quot; & i     dc.DataType = Type.GetType(&quot;System.Boolean&quot;) dc.AllowDBNull = False dt.Columns.Add(dc) Next

'Add 5 rows to the DataTable. For i = 0 To 4 drc = dt.Rows Dim rowVals(4) As Object

'Put values into the Boolean fields. For j = 0 To 4 rowVals(j) = (j Mod 2 = 0) Next

'Add the DataRow. dr = drc.Add(rowVals) Next

'Bind the Datagrid to the DataTable. DataGrid1.DataSource = dt

'Bind the Visible property of the TextBoxes 'to the different Boolean fields from the 'DataTable. TextBox1.DataBindings.Add(&quot;Visible&quot;, dt, &quot;Field1&quot;) TextBox2.DataBindings.Add(&quot;Visible&quot;, dt, &quot;Field2&quot;) TextBox3.DataBindings.Add(&quot;Visible&quot;, dt, &quot;Field3&quot;) TextBox4.DataBindings.Add(&quot;Visible&quot;, dt, &quot;Field4&quot;) TextBox5.DataBindings.Add(&quot;Visible&quot;, dt, &quot;Field5&quot;) End Sub </li> On the Debug menu, click Start.

Note that the Datagrid populates with check boxes, and some of the check boxes are populated with check marks.</li> On different records, click to select several check boxes, click to clear those same check boxes, and then commit the records.</li> Select different cells on different records without changing the values.

Note that the TextBox controls appear and disappear in a random pattern, and they do not correspond to the state of the fields to which they are bound.</li></ol>

Keywords: kbbug kbnofix kbvs2002sp1sweep kbvs2005swept kbvs2005doesnotapply KB327305

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.