Showing posts with label defined. Show all posts
Showing posts with label defined. Show all posts

Friday, March 16, 2012

populating a dropdownlist with data from a multidimensional arrays

Hello,

I hope someone can help me. I have an array defined as a two dimensional array that was populated from a dataset. I want to take that array and populate a dropdownlist. The reason I created a two dimensional array was because in the first dimension I store the value of the dropdownlist and in the second dimension I store the text for the dropdownlist. The value is not just a column from the dataset but a string that is pieced together. Please take a look at the code below and tell me what I'm doing wrong. If someone can please help me. I'm really lost.


'This is were I populate the array
Dim objArray(,) As Object

ReDim objArray(intRowCnt - 1, intColCnt - 1)
i = 0
For Each dr In mDSet.Tables(0).Rows
With mDSet.Tables.Item(0).Rows(i)
If .Item("mbo_key").ToString.Length > 0 Then
objArray(i, 0) = .Item(3).ToString.Trim & "|" & .Item(4).ToString.Trim
objArray(i, 1) = .Item(1).ToString.Trim & " - " & .Item(2).ToString.Trim & " *"
Else
objArray(i, 0) = .Item("period_key").ToString
objArray(i, 1) = .Item(1).ToString.Trim & " - " & .Item(2).ToString.Trim
End If
End With
i += 1
Next

With ddlDropDownList
.DataSource = objArray
For i = 0 To intMBO
' this is were it's blowing up!!!!!
.DataValueField = CStr(objArray(i, 0))
.DataTextField = CStr(objArray(i, 1))
Next
.DataBind()
End With

Why again are you not binding it straight to the Dataset? You should be able to do this using a Dataset, even if it is a concatenation of certain columns (you may even be able to this at the DB level)

As for your problem...you may be able to fix it if instead of using a multidimensial array, use a arrayed class that you define, then you should be able to set the .datavalue/textfield to the class properties (yes use properties). It would look much cleaner too. Very simple class

Public Class Items
Public Property Text...
Public Property Value...
End Class

Hope this helps!
--Michael
I thank you for your help but I'm not sure what you mean. I created 2 Properties like you suggested but I get an error saying I can't convert an array to string when I try to assign it to the .DataValueField of the dropdownlist. Can you please show me an example because I'm really fustrated with this problem.

Also the reason I don't bind it to a dataset is because the value of an option changes depending on if a certain field is Null or not. I wasn't sure how to do this.


With selPeriod
.DataValueField = mclsMBO.marstrValue
.DataTextField = mclsMBO.marstrText
End With

I want to thank you for your help. You set a light off in my head and I ran with it. What I did was I added to Columns to the dataset and populated them with the info I needed it and then I binded those fields to the drop down list. Once again I want to say thanks.

Populating a List Box

Hi

I am trying to populate a list box with all my themes that i have created but I keep getting an message which is

Type DirectoryInfo is not defined i m guessing i have to declare if but i dont know how are where please could you guys help. Thanks

1Protected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs)23If (Not Page.IsPostBack)Then45 Dim themeDirAs New DirectoryInfo(Server.MapPath("App_Themes"))6 lstThemes.DataTextField ="Name"7 lstThemes.DataSource = themeDir.GetDirectories()8 lstThemes.DataBind()910End If1112 End Sub13

The DirectoryInfo class is located under the System.IO namespace.


Yeah i tired declaring it as Inherits System.IO. Is that the correct appraoch


hello i ve tried to use the system io like u suggested but i still have the same problem here s my code. Do u have any ideas where i am going wrong cheers

PartialClass _Default

Inherits System.Web.UI.Page

Inherits System.IO

ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)

If (Not Page.IsPostBack)Then

Dim themeDirAsNew DirectoryInfo(Server.MapPath("App_Themes"))

lstThemes.DataTextField ="Name"

lstThemes.DataSource = themeDir.GetDirectories()

lstThemes.DataBind()

EndIf

EndSub

EndClass


Is your App_Thems directory in your web directory? I would check the path returned by Server.MapPath("App_Theme"), perhaps it doesn't exists and that's why you're getting an error.

bigyawn_hippo:

hello i ve tried to use the system io like u suggested but i still have the same problem here s my code. Do u have any ideas where i am going wrong cheers

PartialClass _DefaultInherits System.Web.UI.Page

Inherits System.IO

ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)

If (Not Page.IsPostBack)Then

Dim themeDirAsNew DirectoryInfo(Server.MapPath("App_Themes"))

lstThemes.DataTextField ="Name"

lstThemes.DataSource = themeDir.GetDirectories()

lstThemes.DataBind()

EndIf

EndSub

EndClass

Hi,

Based on my understanding, you want to import the System.IO namespace in your asp.net VB application. If I have misunderstood you, please feel free to let me know.

The DirectoryInfo class is in the System.IO. If we want to use it, we can import it. For example:

Imports System.IOPartialClass _DefaultInherits System.Web.UI.PageProtected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs)If (Not Page.IsPostBack)Then Dim themeDirAs New DirectoryInfo(Server.MapPath("App_Themes")) lstThemes.DataTextField ="Name" lstThemes.DataSource = themeDir.GetDirectories() lstThemes.DataBind()End If End SubEnd Class

Alternatively, we can use this namespace in the code behind directly, for example:

Dim themeDir As NewSystem.IO.DirectoryInfo(Server.MapPath("App_Themes"))

Besides, you can find more information about asp.net by the following link:http://www.asp.net/get-started/.


I hope this helps.


Hi hyppo,

why don't you try to put only this two instructions ?

lstThemes.DataSource = themeDir.GetDirectories()
lstThemes.DataBind()

I found my code doing the same and i didn' t write ...DataTextField