Solved

How to assign selected value to dropDownList inside gridview?

Posted on 2008-10-16
3
939 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

777 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