Databind multidimensional string array

  • Thread starter Thread starter IGotNothin
  • Start date Start date
I

IGotNothin

Seems like this should be simple, but I'm new to vb.net. I've created
a array of strings. I want to bind them to a dropdown list. I was
able to do this with a one dimensional array with no problem, but with
a separate column for text and value, I can't find how to tell it which
columns to use. Code:

Dim months(,) As String = {{"00", "Select One"}, {"01", "January"},
{"02", "February"}, {"03", "March"}}
expmonth.DataSource = months
expmonth.DataTextField = ??
expmonth.DataValueField = ??
expmonth.DataBind()

So basically I just want to know what to put where the question marks
are. Seems like it should be obvious, but so far everything I've tried
hasn't worked. Could be I'm not creating the array to begin with, but
don't know.

Thanks in advance for any help.
 
Hi,

You can not bind to a multidimensional array. Try creating a class
to hold the info instead.


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

'Put user code to initialize the page here

If Not IsPostBack Then

Dim months As New ArrayList

months.Add(New MyData("00", "Select One"))

months.Add(New MyData("01", "January"))

months.Add(New MyData("02", "Feburary"))

months.Add(New MyData("03", "March"))

DropDownList1.DataSource = months

DropDownList1.DataTextField = "Name"

DropDownList1.DataValueField = "Id"

DropDownList1.DataBind()

End If

End Sub



The class

Public Class MyData

Private mstrId As String

Private mstrName As String

Public Sub New(ByVal iId As String, ByVal sName As String)

mstrId = iId

mstrName = sName

End Sub

Public Property Id() As String

Get

Return mstrId

End Get

Set(ByVal Value As String)

mstrId = Value

End Set

End Property

Public Property Name() As String

Get

Return mstrName

End Get

Set(ByVal Value As String)

mstrName = Value

End Set

End Property

End Class





Ken
----------------------
Seems like this should be simple, but I'm new to vb.net. I've created
a array of strings. I want to bind them to a dropdown list. I was
able to do this with a one dimensional array with no problem, but with
a separate column for text and value, I can't find how to tell it which
columns to use. Code:

Dim months(,) As String = {{"00", "Select One"}, {"01", "January"},
{"02", "February"}, {"03", "March"}}
expmonth.DataSource = months
expmonth.DataTextField = ??
expmonth.DataValueField = ??
expmonth.DataBind()

So basically I just want to know what to put where the question marks
are. Seems like it should be obvious, but so far everything I've tried
hasn't worked. Could be I'm not creating the array to begin with, but
don't know.

Thanks in advance for any help.
 

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

Back
Top