Solved

How to assign selected value to dropDownList inside gridview?

Posted on 2008-10-16
3
941 Views
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?
thanks
<asp:GridView ID="gvReview" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" OnRowDataBound="gvReview_RowDataBound">
                <HeaderStyle CssClass=data_title />
                <Columns>
                                        <asp:TemplateField HeaderText="Review Status">
                        <ItemTemplate>
                            <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>
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                                    </Columns>
                </asp:GridView>
 
 
****************************************
In .cs file:
DataSet dsReview = new DataSet();
dataAdapter.Fill(dsReview);
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

0
Comment
Question by:viola123
3 Comments
 
LVL 20

Accepted Solution

by:
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)
    {
        try
        {
            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

0
 
LVL 1

Expert Comment

by:josgal
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)

0
 
LVL 6

Author Comment

by:viola123
ID: 22753938
informaniac,
thanks a lot, it works
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

790 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