Asp DropDownList

M

Miguel Dias Moura

Hello,

I have two questions concerning Asp:DropDownList

1. How to fill a DropDownList from runtime and set its default item?

2. How do use a DropDownList in each datagrid record?
All dropdownlist should be filled with the values in field NAME from
the same dataset used in the datagrid.
The default value of each dropdownlist should be the value of NAME by
that record.

Thanks,
Miguel
 
K

Ken Cox

Hi Miguel,

Here's a way to add a dropdownlistbox to a grid and select the corresponding
item.

Filling the dropdown could be much more efficient, but this gives you the
idea.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]


<%@ Page Language="VB" %>
<%@ import Namespace="system.data" %>

<script runat="server">
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If

End Sub
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
If (e.Item.ItemType = ListItemType.AlternatingItem) _
Or (e.Item.ItemType = ListItemType.Item) Then
Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim itemID As String = DRV("CatID")
Dim DropDownList1 As DropDownList =
e.Item.FindControl("DropDownList1")
Dim item As ListItem
DropDownList1.DataSource = CreateDataSource()
DropDownList1.DataTextField = "CatName"
DropDownList1.DataValueField = "CatID"
DropDownList1.DataBind()
item = DropDownList1.Items.FindByValue(itemID)
If Not item Is Nothing Then item.Selected = True
End If
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("CatID", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("CatName", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource



</script>

<html >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:DataGrid ID=DataGrid1 runat=server>
<Columns>
<asp:TemplateColumn HeaderText="DDL">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</ItemTemplate>

</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</div>
</form>
</body>
</html>
 

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,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top