When you bind an array to a datagrid, what's the field name?

A

AFN

In VB.NET, I can successfully bind a generic datagrid to a 1 dimensional
array. But, now I want to add a button column, and so I think I need to
make the first column (the one having data in the array) a "BoundColumn" and
the 2nd column my button column. The problem is that the BoundColumn wants
a DataField name. There is none! So how do I bind this one column to the
array column when the array doesn't have column names? And if your answer
is to put column names in my array, please tell me how.
 
J

Jared

Why didn't you just test this on a new page? It looks like the answer you
are looking for is "Item"

'Test
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim test() As String = New String() {"one", "two", "three", "four",
"five"}
Me.DataGrid1.DataSource = test
Me.DataGrid1.DataBind()
End Sub

'Results
Item
one
two
three
four
five
 
A

AFN

I did test that. And I also saw the column name it creates of "Item", but
if you try to bind to "Item" it fails. "Item" is the name it gives to the
header but not really the column name.
 
J

Jared

I see what you mean, try using a custom class, you can use either a
collection or an arraylist.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim Items As New Collection
For counter As Integer = 1 To 10
Dim Item As New MyItems
Item.Item = "Item: " & counter
Items.Add(Item)
Next
Me.DataGrid1.DataSource = Items
Me.DataGrid1.DataBind()
End Sub

Private Class MyItems
Dim mItem As String
Public Property Item() As String
Get
Return mItem
End Get
Set(ByVal Value As String)
mItem = Value
End Set
End Property
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top