How to assign selected value to dropDownList inside gridview?

Posted on 2008-10-16
Last Modified: 2008-10-19
hi all,
i have a grid view which include a drop down list, when every time i pull out data from database, i want to assign the value to that dropdownlist.

i like to do something like this:
if(ds.Tables[0].Rows[0].ItemArray[1].ToString() != "")
      ddlRvStatus.selectedValue = ds.Tables[0].Rows[0].ItemArray[1].ToString() ;

how to do that?
<asp:GridView ID="gvReview" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" OnRowDataBound="gvReview_RowDataBound">
                <HeaderStyle CssClass=data_title />
                                        <asp:TemplateField HeaderText="Review Status">
                            <asp:DropDownList ID="ddlRvStatus" runat="server" CssClass="field_item_width">
                            <asp:ListItem Value="Complete">Complete</asp:ListItem>
                            <asp:ListItem Value="Escalated">Escalated</asp:ListItem>
                            <asp:ListItem Value="Incomplete">Incomplete</asp:ListItem>
In .cs file:
DataSet dsReview = new DataSet();
gvReview.DataSource = ds;
gvReview.DataBind(); // when bind the dataSet, i want to assign selected value(which is in dataset) to that dropdroplist, how to do that?

Open in new window

Question by:viola123
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 20

Accepted Solution

informaniac earned 500 total points
ID: 22738187
In the gridView_RowDataBound Event
u'll have to find the ddl using findcontrol

protected void gvStationery_RowDataBound(object sender, GridViewRowEventArgs e)
            if (e.Row.RowType == DataControlRowType.DataRow)
                    DropDownList ddlRvStatus = e.Row.FindCOntrol("ddlRvStatus") as DropDownList;
ddlRvStatus.Items.FindByValue(ds.Tables[0].Rows[0].ItemArray[1].ToString()).Selected = true;

Open in new window


Expert Comment

ID: 22738718
Just assign or get the value of the cell, as if it was a "text" cell.
.NET, knowing is a comboBox (or dropDownList) will assign the value to its content (in this case, the combo)


Author Comment

ID: 22753938
thanks a lot, it works

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

696 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