Problem using DropDownList while editing in a GridView

I have a gridview on my page that is bound to a DataTable.  One of the fields in the DataTable contains a Y or N to indicate the record is Active or Inactive.  When this field is being editted, I want to have a dropdownlist with the options Active and Inactive for the use to choose from.  The dropdownlist is not populated from a table.  How do I bind my field from my DataTable to the dropdownlist?  Here is my code:

                    <asp:TemplateField HeaderText="Active">
                            <asp:DropDownList ID="DropDownList2" runat="server">
                                <asp:ListItem Value="Y">ACTIVE</asp:ListItem>
                                <asp:ListItem Value="N">INACTIVE</asp:ListItem>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("ActiveStatusFlag") %>'></asp:Label>

Open in new window

I want to have Inactive chosen in the dropdownlist if the ActiveStatusFlag is N and Active if the ActiveStatusFlag is Y.   Any help is greatly appreciated.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
You'll have to do it the old fashioned way as part of the RowEditing event. Something along the lines of:
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            // get currently selected value
            Label label = GridView1.Rows[e.NewEditIndex].FindControl("Active") as Label;
            string currentValue = label.Text;

            // switch grid to edit mode and re-bind
            GridView1.EditIndex = e.NewEditIndex;

            // grab the DropDownList and find the corresponding option
            DropDownList ddl = GridView1.Rows[e.NewEditIndex].FindControl("ddlUsers") as DropDownList;
            ListItem item = ddl.Items.FindByValue(currentValue);

            // set option selected if we found it
            if (item != null)
                item.Selected = true;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dyaroshAuthor Commented:
That worked for setting the value.  How do I access the value in the RowUpdating event for the gridview?  This is what I tried but I am getting an error:

dtOpenTeams.Rows[row.DataItemIndex]["ActiveStatusFlag"] = ((DropDownList)(row.Cells[5].Controls[0])).SelectedValue;

Error: InvalidCastException - Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.DropDownList'.

Before I added the DropDownList and used a TextBox, the following code worked fine:
dtOpenTeams.Rows[row.DataItemIndex]["ActiveStatusFlag"] = ((TextBox)(row.Cells[5].Controls[0])).Text;
dyaroshAuthor Commented:
I ended up using the following code to get the value from the DropDownList in the RowUpdating event:

            DropDownList ddl = gvOpenTeams.Rows[e.RowIndex].FindControl("ddlStatus") as DropDownList;
            dtOpenTeams.Rows[row.DataItemIndex]["ActiveStatusFlag"] = ddl.SelectedValue;

Thanks for your help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.