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

J

Jon Edney

In ASP.NET v2.0, I can successfully bind a generic datagrid to a 1 dimensional
array. But, now I want to customise the column header. I think I need to
make the first column (the one having data in the array) a "BoundColumn" and
set the HeaderText property, but 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?

Example
=======

Code
----
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

It looks like the column name "Item" should work... but it doesn't!

Thanks,
Jon.
 
B

Brock Allen

You'll have to either set a HeaderTemplate for each column or handle the
DataBound event for the header row and change it manually.
 
J

Jon Edney

Hi,

Thanks for your email.

I have tried setting the column header on the DataBound event of the Grid,
but it doesn't believe the automatically generated columns exist. I get an
out of bounds error message. I've checked the code using normally bound
fields, and it works fine.

Could you give me a bit more info on the column template suggestion? I'm not
entierly sure what you are suggesting.

Thanks,
Jon.
 
B

Brock Allen

Just make your template like this (instead of a BoundColumn):

<Columns>
<asp:TemplateColumn>
<HeaderTemplate>Your Header Goes Here</HeaderTemplate>
<ItemTemplate><%# Container.DataItem %></ItemTemplate>
</asp:TemplateColumn>
</Columns>
 

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,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top