Thursday, March 29, 2012

Lookup values and DDL in datagrid

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.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
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" <anonymous@.discussions.microsoft.com> wrote in message
news:FE57EF34-773B-4C79-A9AD-09B79FFC4869@.microsoft.com...
> 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 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.
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
Bin,

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

Vik

"Bin Song, MCP" <anonymous@.discussions.microsoft.com> wrote in message
news:3D2712C3-1CA8-42A3-BAD1-D79A6CEEBD29@.microsoft.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

0 comments:

Post a Comment