I would like to display a field in my GridView as a DropDownList when a user is editing the row. The DropDownList would be pre-populated with two values, "Yes" and "No", and depending on which value a user selects I would like to set a variable.
Example:
I have a field called active. 1 = Active, 0 = Not Active. Although for the user I would like them to either set Active as "Yes" (1) or "No" (0). When when editing a row they could either choose "Yes" or "No" from the drop down and it would set a variable to either 1 or 0, so I can send that back in the SQL update.
I found this MSDN article.
but it only tells me how to populate the DropDownList from a DataSource, which won't work for me since every field has either a yes or no for active. Also it displays a drop down even when just viewing the GridView, not just when editing.
I hope that makes sense, thanks for the help.
EDIT
Here is the code I have now, it almost works like I'd like it to. All I need to do now is change the label text to "Yes" if the value of Active is "True" and change the text to "No" if the value of Active is "False".
<asp:TemplateField HeaderText="Active" SortExpression="Active" >
<ItemTemplate>
<asp:Label ID="lblActive" runat="server" text='<%# Eval("Active") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("Active") %>'>
<asp:ListItem Text="Yes" Value="True"></asp:ListItem>
<asp:ListItem Text="No" Value="False"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
You can add the listitem like...
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Yes" Value="1"></asp:ListItem>
<asp:ListItem Text="No" Value="0"></asp:ListItem>
</asp:DropDownList>
And you can bind the SelectedValue and it will automatically pass the DB a 1 or o.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With