2013-02-25 30 views
2

今天我的客戶的項目的一個工作時,我想出了對此我無法找出解決的問題。所以我把它扔給你們。在ASP.NET DataGrid中實現編輯,當數據來自幾個相關表

我使用數據綁定GridView控件,用於在SqlDataSource爲同一代碼:

<asp:SqlDataSource ID="SqlDataSource_familyMembers" runat="server" 
    ConnectionString="<%$ ConnectionStrings:VBAT_dbConnectionString %>" 
    SelectCommand=" 
     select 
      a.memberid,a.fristName,a.lastname,a.gender,a.dob, 
      b.relation as [Relation with Family Head], 
     c.districtname,d.statename,e.countryname,a.mobno,f.occupation,a.ismaried 
     from 
     family_members_info as a 
     left outer join 
     master_relations as b 
     on 
     a.RelationWithFamilyHead=b.relationid 
     join 
     master_district as c 
     on 
     a.cadistrict=c.districtid 
     join 
     master_state as d 
     on 
     a.castate=d.stateid 
    join 
     master_country as e 
    on 
     a.cacountry=e.countryid 
    join 
     master_occupation as f 
    on 
     a.occupation=f.occupationid 
    where 
     [email protected]" 
    UpdateCommand=" 
     update 
      family_members_info 
     set      
      [email protected], [email protected], [email protected], 
      [email protected],[email protected], 
      [email protected], [email protected],[email protected],[email protected] 
     where 
      [email protected]"> 
    <SelectParameters>      
    <asp:ControlParameter ControlID="HiddenField1" Name="familyid" PropertyName="Value" /> 
    </SelectParameters> 
    <UpdateParameters> 
     <asp:Parameter Name="firstname" /> 
     <asp:Parameter Name="lastName" /> 
     <asp:Parameter Name="dob" /> 
     <asp:Parameter Name="relationwithfamilyhead" /> 
     <asp:Parameter Name="cadistrict" /> 
     <asp:Parameter Name="castate" /> 
     <asp:Parameter Name="cacountry" /> 
     <asp:Parameter Name="mobno" /> 
     <asp:Parameter Name="occupation" /> 
     <asp:Parameter Name="memberID" /> 
    </UpdateParameters> 
</asp:SqlDataSource> 

正如你可以從,數據來自幾個相關的表中選擇查詢看到,一切工作只是罰款當用戶想要編輯數據時。

對於所有列,默認情況下,GridView控件顯示文本框,而應該針對外鍵的列顯示下拉列表。

我將是巨大的幫助,如果任何人都可以引導我爲這個可能的解決方案。

回答

1

達到你要找的內容請按照下列高水平步驟:

  1. 模板網格視圖 - 在您的網格視圖每一列創建TemplateField項目,對於ItemTemplateEditItemTemplate
  2. 對於id字段,指定EditItemTemplate的控制類型爲DropDownList
  3. 創建SqlDataSource或您選擇的任何其他數據綁定方法,並將其綁定到DropDownList來檢索所需的表
  4. 設置DropDownListDataValueField的ID和值是IDDataTextField顯示值

這裏的上述幾點的工作示例 - Filtering Dropdownlist populated from sqldatasource

+0

實現。先生,謝謝你的幫助。 – 2013-02-26 04:21:26