binding to a dropdownlist in a datagrid

  • Thread starter Thread starter John Smith
  • Start date Start date
J

John Smith

Ok so i've added a ddl to a datagrid in vs.net using the template controls
provide in the ide. How then do I bind that ddl to a field in an access
database. Incidently, by binding the control, that means it should drop-down
all values stored in each record or can I limit the values say by a
validation rule in access?
 
btw I only want the values that it could be or do i have to add them
manually? i.e. i don't want the values in the row i want to values that
could be in the row
 
I would create a dataset to populate the ddl.

Eg: I created a dataset called DsOrderType1 and a ddl called dlstOrderType.

To populate the ddl when the page loads I added the following code:

If Not IsPostBack Then
dlstOrderType.DataSource = DsOrderType1
dlstOrderType.DataMember = "tblOrderType"
dlstOrderType.DataTextField = "OrderType"
dlstOrderType.DataBind()
End If

To can refer to the selected item in code as follows:

Dim OrderType = dlstOrderType.SelectedValue
 
thx for the reply. The ddl is in the ItemTemplate column in a datagrid.
dlstOrderType.databind() would not work.
I need to know hwo to bind when embedded in the datagrid. Also I need it to
display 5 values that are the domain in a field in a access db. so say cat,
dog, mouse, sheep, frog. However when the datgrid displays data from a db i
need it to show the selected animal and all the rest of the fields to just
have the 5 fields available to pick. Sounds more complicated than i make it.


cheers
 
Hi,

Its actually quite simple.....here are the steps:

1.

on the page codes behind (mypage.aspx.vb)
create a public varaiable.
Protected DropDownDataView As DataView = New DataView

Then create a function/sub that will load this dataview with data.

Private Sub LoadDropDownDataView()
Dim supplier As New VendimaxBOL.Supplier
Dim dt As DataTable
dt = supplier.SupplierGetAll
DropDownDataView = New DataView(dt)
End Sub

2.

In the (myPage.aspx), create teh desired column where the dropdown will
appear as a TemplateColumn

<asp:TemplateColumn HeaderText="Supplier">
<HeaderStyle ForeColor="White"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.supplier") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlAddSuppliers" Runat="server"
DataTextField="SupplierName" DataValueField="ID"
DataSource="<%#DropDownDataView %>" CssClass="dropdown_medium">
</asp:DropDownList>
</FooterTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlEditSuppliers" Runat="server"
DataTextField="SupplierName" DataValueField="ID"
DataSource="<%#DropDownDataView %>" CssClass="dropdown_medium">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>



3. Now for the last part, when you go into edit mode, get the currently
selected item in the dropdown list to be autoselected

Private Sub dgProducts_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgProducts.EditCommand
Me.dgProducts.EditItemIndex = e.Item.ItemIndex
'insert code to reload your datagrid here
Dim prod As New
myBOL.Product(Convert.ToInt32(CType(e.Item.Cells(0).Controls(1),
Label).Text))
Try
CType(dgProducts.Items(e.Item.ItemIndex).Cells(2).Controls(1),
DropDownList).SelectedValue = prod.SupplierID 'Sets the selected supplier
Catch ex As Exception
End Try
End Sub


Hope this helps!!!
 
Back
Top