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

Datagrid with Yes/No Dropdownlist

P: n/a
I have added a dropdownlist to an editable datagrid to allow a user to select
Yes or No:

<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem,
"reports") %>' ID="lblReports"/>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" id="ddlReports">
<asp:ListItem value="Yes">Yes</asp:listItem>
<asp:ListItem value="No">No</asp:listItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>

Works great.

My problem occurs when the user clicks the edit button. The value always
comes up to "Yes" in the dropdownlist, even if the value in the datagrid
label was "No".

How do I code it to make the value in the dropdownlist match the value in
the datagrid label before Edit was selected ?

Thanks,
Jeff
Dec 1 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Add a DataBinder expression for the SelectedValue. Assuming that the field
named "Reports" holds a string value of either "Yes" or "No" then:

<asp:DropDownList ID="ddlReports" Runat="server"
SelectedValue='<%# DataBinder.Eval(Container.DataItem, "reports") %>'>
<asp:ListItem value="Yes">Yes</asp:listItem>
<asp:ListItem value="No">No</asp:listItem>
</asp:DropDownList>

But if the field value can have a blank or any other value, you need to
create a condition, e.g. (C# syntax)
<asp:DropDownList ID="ddlReports" Runat="server"
SelectedValue='<%# DataBinder.Eval(Container.DataItem,
"reports").Equals("Yes")?"Yes","No" %>'>
<asp:ListItem value="Yes">Yes</asp:listItem>
<asp:ListItem value="No">No</asp:listItem>
</asp:DropDownList>
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Jeff" wrote:
I have added a dropdownlist to an editable datagrid to allow a user to select
Yes or No:

<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem,
"reports") %>' ID="lblReports"/>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" id="ddlReports">
<asp:ListItem value="Yes">Yes</asp:listItem>
<asp:ListItem value="No">No</asp:listItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>

Works great.

My problem occurs when the user clicks the edit button. The value always
comes up to "Yes" in the dropdownlist, even if the value in the datagrid
label was "No".

How do I code it to make the value in the dropdownlist match the value in
the datagrid label before Edit was selected ?

Thanks,
Jeff

Dec 1 '05 #2

P: n/a
Thanks Phillip..

That did the trick... I appreciate your help.

Jeff

Dec 1 '05 #3

P: n/a
Jeff,

On ItemEdit event of DataGrid add this code

Dim ddlList as DropDownList
ddlList = e.Item.FindControl("ddlReports")

If Not ddlList Is Nothing Then
ddlList.selectedValue = e.Item.DataItem("ddlReports")
END If
HTH

Nilesh


"Jeff" <Je**@discussions.microsoft.com> wrote in message
news:AA**********************************@microsof t.com...
I have added a dropdownlist to an editable datagrid to allow a user to
select
Yes or No:

<ItemTemplate>
<asp:Label runat="server" Text='<%#
DataBinder.Eval(Container.DataItem,
"reports") %>' ID="lblReports"/>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" id="ddlReports">
<asp:ListItem value="Yes">Yes</asp:listItem>
<asp:ListItem value="No">No</asp:listItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>

Works great.

My problem occurs when the user clicks the edit button. The value always
comes up to "Yes" in the dropdownlist, even if the value in the datagrid
label was "No".

How do I code it to make the value in the dropdownlist match the value in
the datagrid label before Edit was selected ?

Thanks,
Jeff

Dec 1 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.