Displaying a drop-down list when editing a GridView

I would like to display the field in my GridView as a DropDownList when the user edits a row. DropDownList will be pre-populated with two values: โ€œYesโ€ and โ€œNoโ€, and depending on what value the user selects, I would like to set a variable.

Example:

I have a field called active. 1 = active, 0 = inactive. Although for the user, I would like them to either set Active as โ€œYesโ€ (1) or โ€œNoโ€ (0). When, when editing a line, they could either select โ€œYesโ€ or โ€œNoโ€ from the drop-down list, and it will set the variable to 1 or 0, so I can send it back to the SQL update.

I found this MSDN article .

but it only tells me how to populate a DropDownList from a DataSource, which will not work for me, since each field has either yes or no for the active. It also displays a drop, even with a simple view of the GridView, and not just when editing.

I hope this makes sense, thanks for the help.

EDIT

Here is the code that I have now, it almost works as I would like. All I have to do is change the label text to โ€œYesโ€ if the Active value is โ€œTrueโ€ and change the text to โ€œNoโ€ if the Active value 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>
+3
source share
5 answers

You can add listitem as ...

<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 SelectedValue, and it will automatically transmit DB a 1 or o.

+1

EditTemplate , ItemTemplate . msdn post ( ).

0
  • ( ) edititem

    <asp:Label ID="lbl" runat="server" Text='<%#Eval("authostatus") %>' Visible="false"></asp:Label>
    <asp:DropDownList ID="Autharisationddl" runat="server">
      <asp:ListItem Text="Yes" Value="1"></asp:ListItem>
      <asp:ListItem Text="No" Value="0"></asp:ListItem>
    </asp:DropDownList>
    
  • rowdatabound gridview .

    if ((e.Row.RowState & DataControlRowState.Edit) > 0)
    {
      Label lbl= (Label)e.Row.FindControl("lbl");
      DropDownList ddl= (DropDownList)e.Row.FindControl("ddl");
    
      if (lbl!= null)
      {
        if (lbl.Text == "1")
          ddl.SelectedValue = "1";
        else if (lbl.Text == "0")
          ddl.SelectedValue = "0";
      }
    }
    

0

rowdataound, .

0
0

All Articles