anarhikos
anarhikos

Reputation: 53

SelectedValue of DropDownlList

I have a gridview that has dropdownlist in edit section, I want to bind the selectedvalue from database when editing. In designer section there is no SelectedValue attribute, it gives runtime error. What to do any help?? Is there any way to handle it from code-behind?

<asp:TemplateField HeaderText="Company">
                <EditItemTemplate>
                    <asp:DropDownList ID="DDLCompany" runat="server" DataValueField="cname" DataTextField="cname" SelectedValue = '<%# Bind("cname") %>'  >
                    </asp:DropDownList>
                </EditItemTemplate>
                <ItemTemplate>
                <asp:Label ID="CompanyLabel" runat="server" Text='<%# Bind("cname") %>'></asp:Label>
                </ItemTemplate>   
             </asp:TemplateField>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList DDLCompany = (DropDownList)e.Row.FindControl("DDLCompany");
            DropDownList DDLPrinter = (DropDownList)e.Row.FindControl("DDLPrinter");

            if (DDLCompany != null)
            {
                DDLCompany.DataSource = userobj.FetchCompanyList();
                DDLCompany.DataBind();
                DDLCompany.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString();                
            }

            if (DDLPrinter != null)
            {
                DDLPrinter.DataSource = userobj.FetchPrinterList();
                DDLPrinter.DataBind();
                DDLPrinter.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString();
            }
        }
    } 

Upvotes: 1

Views: 253

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460340

Upvotes: 1

Related Questions