Problem using DropDownList while editing in a GridView

Posted on 2013-09-25
Medium Priority
Last Modified: 2013-09-25
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.
Question by:dyarosh
  • 2
LVL 52

Accepted Solution

Carl Tawn earned 2000 total points
ID: 39520962
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


Author Comment

ID: 39521118
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;

Author Closing Comment

ID: 39521152
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!

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question