Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 973
  • Last Modified:

How to assign selected value to dropDownList inside gridview?

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
viola123
Asked:
viola123
1 Solution
 
informaniacCommented:
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
 
josgalCommented:
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
 
viola123Author Commented:
informaniac,
thanks a lot, it works
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now