Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGrid Formatting when bound to ArrayList?
 

DataGrid Formatting when bound to ArrayList?

How do i control the formatting of a datagrid when i bind to an arraylist?


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim al1 As New ArrayList()

al1.Add("Apple")

al1.Add("Orange")

al1.Add("Bacon")

'Create a DataGridTableStyle set mappingname to table.

Dim ts1 As New DataGridTableStyle()

ts1.MappingName = "?"

'Step 2: Amount

Dim column As New DataGridTextBoxColumn()

column.MappingName = "?"

column.HeaderText = "FOOOD"

ts1.GridColumnStyles.Add(column)

dg1.TableStyles.Add(ts1)

dg1.DataSource = al1

End Sub


 

plucky  Friday, September 09, 2005 9:35 PM

The good news: when you're bound to an ArrayList, it works like you're bound to anything else -- you can set the TableStyle's MappingName to the type name ("ArrayList"), and all the public properties of the type are available as columns.

The bad news: string isn't special cased: string has one public property, and it's Length.  If you don't set up TableStyles or ColumnStyles, that's what's available.  ColumnStyles and TableStyles let you hide properties that you'd normally see, but don't let you add new properties.

There are a couple easy workarounds here.  One is to create a DataTable that holds your items instead of an ArrayList.  The other is to create a class that holds your strings and exposes them as a public property:

Public Class StringWrapper
  Private MyValue As String

  Sub
New(ByVal s As String)
    MyValue = s
  End Sub

  Public
ReadOnly Property Value() As String
    Get
      Return MyValue
    End Get
  End Property
End Class

   -Scott

Scott Berry  Wednesday, September 21, 2005 6:29 PM

The good news: when you're bound to an ArrayList, it works like you're bound to anything else -- you can set the TableStyle's MappingName to the type name ("ArrayList"), and all the public properties of the type are available as columns.

The bad news: string isn't special cased: string has one public property, and it's Length.  If you don't set up TableStyles or ColumnStyles, that's what's available.  ColumnStyles and TableStyles let you hide properties that you'd normally see, but don't let you add new properties.

There are a couple easy workarounds here.  One is to create a DataTable that holds your items instead of an ArrayList.  The other is to create a class that holds your strings and exposes them as a public property:

Public Class StringWrapper
  Private MyValue As String

  Sub
New(ByVal s As String)
    MyValue = s
  End Sub

  Public
ReadOnly Property Value() As String
    Get
      Return MyValue
    End Get
  End Property
End Class

   -Scott

Scott Berry  Wednesday, September 21, 2005 6:29 PM

You can use google to search for other answers

Custom Search

More Threads

• UserControl and MessageBox.Show - Does not show How do I do it ?
• DataGridView inside UserControl auto removing Friend WithEvents modifier
• Custom made Class in a Cell?
• Writing event in Datagridviewcombobox?
• How can I validate the textbox control
• Check box data binding, AddNew doesn't work
• Need help populating a DropDownList in and EditItemTemplate
• Link a cbo to a datagrid based on distinct values in a dataset
• AddNew (BindingSource)
• Initialise a cell in a new row