Microsoft KB Archive/820652

= BUG: A DataGrid control that you bind to an ArrayList class in a Windows Form displays the length of the ArrayList members instead of the ArrayList items =

Article ID: 820652

Article Last Modified on 2/3/2006

-

APPLIES TO


 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition

-



SYMPTOMS
When you bind a DataGrid control to an ArrayList class in a Microsoft Windows Form application, the DataGrid displays the length of the ArrayList members, instead of displaying the ArrayList items.



WORKAROUND
To work around this bug, create a wrapper class that returns the ArrayList items when the Length property is called. To do this, follow these steps:  Write a class to wrap the members. To do this, follow these steps:  Open your existing project in Microsoft Visual Studio .NET. Right-click  . Point to Add, and then click Add Class.   Replace the existing code with the following code:

Microsoft Visual Basic .NET Code

Public Class dimension Private m_length As String Public Sub New(ByVal str As String) m_length = str End Sub

Public Property Length As String Get Length = m_length End Get Set(ByVal Value As String) m_length = Value End Set End Property End Class Microsoft Visual C# .NET Code public class dimension {   private string m_length;

public dimension(string str) {           m_length=str; }       public string Length {           get {               return m_length; }           set {               m_length=value; }       }    }   Copy the following code to the Form1_Load event:

Visual Basic .NET Code Dim al As New ArrayList al.Add(New dimension(&quot;one&quot;)) al.Add(New dimension(&quot;two&quot;)) al.Add(New dimension(&quot;three&quot;)) DataGrid1.DataSource = al Visual C# .NET Code ArrayList al= new ArrayList;

al.Add(new dimension(&quot;one&quot;)); al.Add(new dimension(&quot;two&quot;)); al.Add(new dimension(&quot;three&quot;)); dataGrid1.DataSource=al; </li></ol>

<div class="status_section">

STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

<div class="moreinformation_section">

Steps to Reproduce the Behavior
 Create a new Windows application by using Visual Basic .NET or Visual C# .NET.

By default, Form1 is created.</li> Drag a DataGrid control to Form1.</li>  Add the following code to the Form1_Load event:

Visual Basic .NET Code ' Create an ArrayList. Dim al As New ArrayList al.Add(&quot;one&quot;) al.Add(&quot;two&quot;) al.Add(&quot;three&quot;) DataGrid1.DataSource = al Visual C# .NET Code ' Create an ArrayList. ArrayList al= new ArrayList; al.Add(&quot;one&quot;); al.Add(&quot;two&quot;); al.Add(&quot;three&quot;); dataGrid1.DataSource=al; </li> On the Debug menu, click Start to run the application.</li> Verify that the DataGrid displays the length of the text as 3, 3, and 5, instead of displaying the actual text.</li></ol>

<div class="references_section">

For additional information about binding a DataGrid control to an ArrayList, click the following article numbers to view the articles in the Microsoft Knowledge Base:

316303 HOW TO: Bind a DataGrid Control to an ArrayList of Objects or Structures by Using Visual C# .NET

316302 HOW TO: Bind a DataGrid Control to an ArrayList of Objects or Structures by Using Visual Basic .NET

Keywords: kbbug kbwindowsforms kbuidesign kbctrl kbcontrol kbvs2002sp1sweep kbvs2005swept kbvs2005doesnotapply KB820652

-

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

© Microsoft Corporation. All rights reserved.