Lookup values and DDL in datagrid

V

Vik

How can I display the lookup values in a datagrid?
E.g., the datagrid displays a table that contains a ProductID field. I want
to display a Product description from a Product table in that datagrid.
Currently I use a join between the main table and the lookup table to get
the Product description.
Is it possible to use a dropdownlist in the datagrid to display and select
(in Edit mode) the lookup values?

Thank you.
 
G

Guest

Hi, Vik

You can try use TemplateColumn to create a dropdown for your products

<asp:TemplateColumn HeaderText="Product"><ItemTemplate><asp:dropdownlist ID="ddlProduct" Runat="Server"></asp:dropdownlist></ItemTemplate></asp:TemplateColumn

In ItemDataBound Event of the datagrid, provide data for the list

Dim ctl As Control = e.Item.Cells(0).FindControl("ddlProduct"
If Not ctl Is Nothing The
Dim ddl As DropDownList = CType(ctl, DropDownList
With dd
.DataSource = ds 'ds is a dataset created from tblProduc
.DataTextField = "ProductID
.DataValueField = "ProductDescription
.DataBind(
End Wit
End I

Bin Song, MC

----- Vik wrote: ----

How can I display the lookup values in a datagrid
E.g., the datagrid displays a table that contains a ProductID field. I wan
to display a Product description from a Product table in that datagrid
Currently I use a join between the main table and the lookup table to ge
the Product description
Is it possible to use a dropdownlist in the datagrid to display and selec
(in Edit mode) the lookup values

Thank you
 
V

Vik

Thank you, Bin.

I already created TemplateColumn and placed the dropdownlists in
ItemTemplate and EditItemTemplate and set up ddls' properties in design
view. I also set up SelectedValue='<% DataBinder.Eval(Container,
"DataItem.ProductID") %>'.
The datagrid displays correct products, but when trying to edit the
datagrid, I get an error "Specified argument was out of the range of valid
values. Parameter name: value" in dg_EditCommand sub.
If I remove SelectedValue binding, the error doesn't appear, but datagrid,
of course, doesn't display correct values. And in Edit mode ddl is empty.
I tried your code (with an addition of DataMember property) but ddl still is
empty in Edit mode.

Vik

Bin Song said:
Hi, Vik,

You can try use TemplateColumn to create a dropdown for your products:

<asp:TemplateColumn HeaderText="Product"><ItemTemplate><asp:dropdownlist
ID="ddlProduct"
 
G

Guest

Hi Vik

You can not just simply bind the value to the selected value of dropdownlist.
Remove the binding code and in itemdatabound event, add the following code

ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(DataBinder.Eval(Container,"DataItem.ProductID"))

Bin Song, MCP
 
V

Vik

Bin,

If I place this statement in code, I get an error "Name 'Container' is not
declared".

Vik
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top