dv1 = CType(sdsWizard.Select(DataSourceSelectArguments.Empty), DataView)
Dim li As ListItem = New ListItem
For Each r As DataRowView In dv1
li.Text = r("ProjectName").ToString
li.Value = r("ID").ToString
ddlWProjectName.Items.Add(li)
Next
adds the correct number of items to the dropdownlist, but every
item in the list is the last one:
For example, instead of
- Item 1
- Item 2
- Item 3
I get
- Item 3
- Item 3
- Item 3
Try like this: if you can, use dropdownlist
DropDownList li = new DropDownList();
li.datasource=dv1;
li.datatextfield="Projectname";
li.datavaluefield="Id";
li.databind();
Maybe you can use
ddlWProjectName.DataSourceId="sdsWizard"
set ddl DataTextField and DataValueField then call DataBind() Or
dv1 = CType(sdsWizard.Select(DataSourceSelectArguments.Empty), DataView)
ddlWProjectName.DataSource = dv1.Table
ddlWProjectName.DataTextField = "ProjectName"
ddlWProjectName.DataValueField = "ID"
ddlWProjectName.DataBind()
spokey,
actully you are only creating only one li for all the gridView rows, as your Dim li = New ListItem is out-side the loop just change your code as,
dv1 = CType(sdsWizard.Select(DataSourceSelectArguments.Empty), DataView)
Dim li As ListItem = Nothing
For Each r As DataRowView In dv1
li = New ListItem
li.Text = r("ProjectName").ToString
li.Value = r("ID").ToString
ddlWProjectName.Items.Add(li)
Next
hello
spokey go to this link
http://simplyaspnet.blogspot.in/2013/07/how-to-populate-dropdown-using-database.html
thanks
manish
Thanks ijaz! works like a charm now.
pooja and oned, thanks for the suggestion but databinding didn't change anything.
manish- that link is a good resource of a cleaner way to do it, I'll reference it for next time around (which I'm sure will be soon) :)
spokey
that link is a good resource of a cleaner way to do it, I'll reference it for next time around (which I'm sure will be soon) :)
happy to help
thanks
manish
沒有留言:
張貼留言