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



The information in this article applies to:

  • Microsoft .NET Framework Class Libraries 1.1
  • Microsoft .NET Framework Class Libraries 1.0
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

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 "Applies to" section.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Visual Basic .NET, and then open a new project.

    By default, Form1 is created.
  2. Add a DataGrid control to the form.
  3. Add five TextBox controls to the form.
  4. On the View menu, click Code to view the Code window of the form.
  5. 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 = "Field" & 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 = "Field" & i
          dc.DataType = Type.GetType("System.Boolean")
          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("Visible", dt, "Field1")
       TextBox2.DataBindings.Add("Visible", dt, "Field2")
       TextBox3.DataBindings.Add("Visible", dt, "Field3")
       TextBox4.DataBindings.Add("Visible", dt, "Field4")
       TextBox5.DataBindings.Add("Visible", dt, "Field5")
    End Sub
    					
  6. 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.
  7. On different records, click to select several check boxes, click to clear those same check boxes, and then commit the records.
  8. 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.

Modification Type:MinorLast Reviewed:1/20/2006
Keywords:kbvs2005swept kbvs2005doesnotapply kbvs2002sp1sweep kbbug kbnofix KB327305