2014-06-20 59 views
0

從我的數據庫中查詢數據庫後,我正在回顧下面的數據表。 我的asp.net網頁表單應用程序我有GridView,它有BoundField和DropDownList。將數據表多行數據綁定到DropDownList

我想將LectureID綁定到BoundField和派生講座的主題到DropDownList中。

不知道如何去做所需的配合。

LectureID SubjectName 
1   Sub1 
1   Sub2 
1   Sub3 
1   Sub4 
2   Sub1 
2   Sub4 
+0

你檢查,給出的答案? – Hassan

回答

0
<asp:GridView ID="grdvDetail" runat="server"> 
    <Columns> 
     <asp:BoundField HeaderText="LectureID" DataField="LectureID" SortExpression="LectureID"> 
      <HeaderStyle HorizontalAlign="Center" /> 
      <ItemStyle Font-Size="11px" /> 
     </asp:BoundField> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:DropDownList ID="ddlSubject" runat="server"> 
       </asp:DropDownList> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 



protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     DataTable dt = new DataTable(); 
     //Fill DataTable Using SQL Query 
     grdvDetail.DataSource = dt; 
     grdvDetail.DataBind(); 

     for (int i = 0; i < grdvDetail.Rows.Count; i++) 
     { 
      DropDownList ddlSubject = ((DropDownList)grvProduct.Rows[i].FindControl("ddlSubject")); 

      DataTable dtDDL = new DataTable(); 
      //Fill DataTable Using SQL Query 
      ddlSubject.DataSource = dtDDL; 
      ddlSubject.DataTextField = "SubjectName"; 
      ddlSubject.DataValueField = "SubjectName"; 
      ddlSubject.DataBind(); 
     } 
    } 
} 
0

可能是不完美的解決方案。想法是從給定的數據表創建一個Key,Value集合。

A Dictionary集合用於保留演講ID爲KeyList<string>中的相關主題爲Value

GridView是bind with Dictionary和GridView的RowDataBound event每個dropdownlist是根據需要填充。

標記

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
      onrowdatabound="GridView1_RowDataBound"> 
     <Columns> 
      <asp:BoundField DataField="Key" HeaderText="Lecture ID" /> 

      <asp:TemplateField HeaderText="Subject Name"> 
       <ItemTemplate>        
        <asp:DropDownList ID="ddlSubject" runat="server" > 
        </asp:DropDownList> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
</asp:GridView> 

代碼隱藏

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     //test data 
     DataTable dt = new DataTable(); 
     dt.Columns.Add("LectureID", typeof(int)); 
     dt.Columns.Add("SubjectName"); 

     dt.Rows.Add(1, "Sub1"); 
     dt.Rows.Add(1, "Sub2"); 
     dt.Rows.Add(1, "Sub3"); 
     dt.Rows.Add(1, "Sub4"); 

     dt.Rows.Add(2, "Sub1"); 
     dt.Rows.Add(2, "Sub4");    
     dt.AcceptChanges();    

     //Bind with GridView with Dictionary collection 
     GridView1.DataSource = GetDictionary(dt); 
     GridView1.DataBind(); 
    } 
} 

//get a dictionary of distinct lectureID 
private Dictionary<int, List<string>> GetDictionary(DataTable dt) 
    { 
     var dictionary = new Dictionary<int, List<string>>(); 
     foreach (DataRow dr in dt.Rows) 
     { 
      int iKey = Convert.ToInt32(dr["LectureID"]); 

      if (dictionary.ContainsKey(iKey)) 
      { 
       List<string> lst = dictionary[iKey] as List<string>; 
       lst.Add(dr["SubjectName"].ToString()); 
       dictionary[iKey] = lst; 
      } 
      else 
      { 
       var lst = new List<string>(); 
       lst.Add(dr["SubjectName"].ToString()); 
       dictionary.Add(iKey, lst); 
      } 
     } 
     return dictionary; 
    } 

行數據綁定事件

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow && 
      e.Row.DataItem != null) 
     { 
      DropDownList ddl = e.Row.FindControl("ddlSubject") as DropDownList; 
      ddl.DataSource = ((KeyValuePair<int, List<string>>)e.Row.DataItem).Value; 
      ddl.DataBind(); 
     } 
    } 

結果與相應的主題鮮明LectureID在DropDownList中

enter image description here