2012-10-08 52 views
1

我們希望顯示在GridView供應商和每個顯示的產品的產品的項目符號列表。Datasource需要鏈接供應商與每個供應商的產品

例如:

Supplier One    Product A 
          Product B 

Supplier Two    Product A 
          Product B 
          Product C 

這是在GridView的樣子:

 <asp:GridView 
      ID="GridView1" 
      runat="server" 
      AutoGenerateColumns="False" 
      CssClass="DataWebControlStyle"> 

      <HeaderStyle CssClass="HeaderStyle" /> 

      <AlternatingRowStyle CssClass="AlternatingRowStyle" /> 

      <Columns> 
       <asp:BoundField 
        DataField="CompanyName" 
        HeaderText="Supplier" /> 

       <asp:TemplateField HeaderText="Products"> 
        <ItemTemplate> 
         <asp:BulletedList 
          ID="BulletedList1" 
          runat="server" 
          DataSource='<%# %>' 
          DataTextField="ProductName"> 
         </asp:BulletedList> 
        </ItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
     </asp:GridView> 

這是代碼隱藏文件中的數據加載到GridView控件:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 

    Dim suppliersAdapter As New SuppliersTableAdapter 

    GridView1.DataSource = suppliersAdapter.GetSuppliers() 
    GridView1.DataBind() 
End Sub 

你可以告訴我們在asp:BulletedList DataSouce中放置什麼,這樣產品也能顯示出來嗎?

*更新*

感謝大家的幫助。我刪除了DataSource ='<%#%>'這一行,這裏是基於你在代碼隱藏文件中的幫助,現在的額外工作編碼。

因爲我在學習ASP.Net,所以我在代碼中添加了很多註釋,以幫助我瞭解正在發生的事情以及是什麼使它發生變化。如果我在評論中犯了錯誤,請調整評論。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 

    ' TableAdapter object. 
    ' Provide communication between this application and the database. 
    '----------------------------------------------------------------- 
    Dim suppliersAdapter As New SuppliersTableAdapter 

    ' Get the data from the TableAdapter into the GridView. 
    '------------------------------------------------------ 
    GridView1.DataSource = suppliersAdapter.GetSuppliers() 

    ' Display the result set from the TableAdapter in the GridView. 
    '-------------------------------------------------------------- 
    GridView1.DataBind() 
End Sub 

Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound 

    ' A GridView has DataRows, EmptyDataRows, Footers, Headers, Pagers, and Separators. 
    ' The DataSource will be assigned to the BulletedList in this GridView only when the RowType is a DataRow. 
    ' This will make sure the "Object reference not set to an instance of an object" error will not be thrown. 
    '--------------------------------------------------------------------------------------------------------- 
    If e.Row.RowType = DataControlRowType.DataRow Then 

     ' TableAdapter object. 
     ' Provide communication between this application and the database. 
     '----------------------------------------------------------------- 
     Dim productsAdapter As New ProductsTableAdapter 

     ' BulletedList object. 
     ' This object is created from the BulletedList control in the aspx file for GridView 
     ' so it can be used to assign a DataSource to the BulletedList. 
     '------------------------------------------------------------------------------------ 
     Dim BulletedList1 As BulletedList = DirectCast(e.Row.FindControl("BulletedList1"), BulletedList) 

     ' Get the SupplierID into a variable for use as a parameter for the GetProductsBySupplierID method of the TableAdapter. 
     '---------------------------------------------------------------------------------------------------------------------- 
     Dim supplier = DataBinder.Eval(e.Row.DataItem, "SupplierID") 

     ' Get the data from the TableAdapter into the GridView. 
     '------------------------------------------------------ 
     BulletedList1.DataSource = productsAdapter.GetProductsBySupplierID(supplier) 

     ' Display the result set from the TableAdapter in the GridView. 
     '-------------------------------------------------------------- 
     BulletedList1.DataBind() 
    End If 
End Sub 

回答

1

你必須爲每個列表不同的數據源。

你可以通過在網格的RowDataBound事件上設置BulletedList上的數據源來實現你的目標。

附上RowDataBound事件處理程序如下:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType==DataControlRowType.DataRow) 
    {  
     BulletedList BulletedList1 = (BulletedList)e.Row.FindControl("BulletedList1"); 
     var supplier = DataBinder.Eval(e.Row.DataItem, "CompanyName"); 
     BulletedList1.DataSource = GetProductListForSupplier(supplier); 
     BulletedList1.DataBind(); 
    } 
} 
+0

感謝大家的幫助。 :-)我根據你的幫助更新了我的帖子。 –

0

試試這個

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) 
    { 

    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
     // Display the company name in italics. 
     string supplierName = e.Row.Cells[0].Text; 
     //OR assuming you stored the SupplierID in hidden view - this can also be retrieved from GridView DataKeys value for each row 
     string supplierID = ((HiddenField) e.Row.FindControl("hiddenFieldSupplierID")).Value; 

     DataTable products = getProductsBySupplier(supplierID); //or by SupplierName 
     BulletedList BulletedList1 = ((BulletedList) e.Row.FindControl("BulletedList1")) 
     BulletedList1.DataSource = products; 
     BulletedList1.DataBind()  

    } 

    }