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>
"Miguel Dias Moura" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in
message news:Og**************@TK2MSFTNGP12.phx.gbl...
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