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 (820652)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • 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:
  1. Write a class to wrap the members. To do this, follow these steps:
    1. Open your existing project in Microsoft Visual Studio .NET.
    2. Right-click project name.
    3. Point to Add, and then click Add Class.
  2. 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;
    			}
    		}
    	}
  3. Copy the following code to the Form1_Load event:

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

STATUS

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

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a new Windows application by using Visual Basic .NET or Visual C# .NET.

    By default, Form1 is created.
  2. Drag a DataGrid control to Form1.
  3. Add the following code to the Form1_Load event:

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


Modification Type:MinorLast Reviewed:2/3/2006
Keywords:kbvs2005swept kbvs2005doesnotapply kbvs2002sp1sweep kbControl KbUIDesign kbWindowsForms kbCtrl kbbug KB820652 kbAudDeveloper