By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,726 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Lookup values and DDL in datagrid

P: n/a
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.
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi, Vik

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

<asp:TemplateColumn HeaderText="Product"><ItemTemplate><asp:dropdownli st 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

Nov 18 '05 #2

P: n/a
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, MCP" <an*******@discussions.microsoft.com> wrote in message
news:FE**********************************@microsof t.com...
Hi, Vik,

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

<asp:TemplateColumn HeaderText="Product"><ItemTemplate><asp:dropdownli st 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 Then
Dim ddl As DropDownList = CType(ctl, DropDownList)
With ddl
.DataSource = ds 'ds is a dataset created from tblProduct
.DataTextField = "ProductID"
.DataValueField = "ProductDescription"
.DataBind()
End With
End If

Bin Song, MCP

----- 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 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.

Nov 18 '05 #3

P: n/a
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
Nov 18 '05 #4

P: n/a
Vik
Bin,

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

Vik

"Bin Song, MCP" <an*******@discussions.microsoft.com> wrote in message
news:3D**********************************@microsof t.com...
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

Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.