2010-07-01 28 views
1

我有一个在编辑部分有dropdownlist的gridview,我想在编辑时绑定来自数据库的selectedvalue。在设计器部分没有SelectedValue属性,它给运行时错误。怎么做什么帮助?有没有办法从代码隐藏中处理它?DropDownlList的SelectedValue

<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(); 
      } 
     } 
    } 
+0

貌似后面的代码设置为做到这一点... – cjk 2010-07-01 07:29:51

+0

不,它不需要设置。它选择DDL的第一个数据。它不绑定来自数据库的实际值 – anarhikos 2010-07-01 07:39:50

回答