2010-07-01 20 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

回答