Solved

How to assign selected value to dropDownList inside gridview?

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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
Please explain purpose of GZIP 4 59
WPF Select Items 3 25
Using sample Autorize.net c# simple example 1 26
How to set focus on a dynamic control 18 23
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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