2010-07-27 23 views
0

任何人都可以請賜教我有關如何綁定到ASP.Net 4中的GridView的情況下,我的GridView的第一行應該是標題,第二個應該是一個組合框對於每一列和第三個是我的實際數據源的開始。GridView綁定與標題中的組合框

如果你可以想象我想要實現的是在數據網格中的每一列與另一個數據源之間創建綁定的能力。該綁定是由用戶在組合框中選擇一個值創建的。然而,無論我嘗試什麼,我都無法做到這一點。

HeaderText1 | HeaderText2 | HeaderText3 
ComboBox1 | ComboBox2 | ComboBox3 
DataRow1 | DataRow1 | DataRow1 
DataRow2 | DataRow2 | DataRow2 
DataRow3 | DataRow3 | DataRow3 

回答

0

因此,對於任何人都好奇,這似乎是解決問題的辦法。

Private Sub grdMainGrid_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdMainGrid.RowCreated 
    If e.Row.RowType = DataControlRowType.Header Then 
     For Each itm As TableCell In e.Row.Cells 
      itm.Text = GenerateHeaderHTML() 
     Next 
    End If 
End Sub 

PS:如果任何人有任何更好的解決方案我很想聽到他們:-)

下面的代碼是我在GenerateHeaderHTML()。我的代碼是一個非常特殊的情況(並且很不錯)。但請注意,您可以使用任何您希望的html。

Private Sub grdMainGrid_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdMainGrid.RowCreated 
      If Me.BoundedObjects IsNot Nothing Then 
       If e.Row.RowType = DataControlRowType.Header Then 
        Dim PrimitivePropertyNames As List(Of String) = ParserHelper.GetPrimitivePropertyNames(Me.BoundedObjects.ToList) 
        Dim i As Integer = 0 
        For Each itm As TableCell In e.Row.Cells 
         itm.Text = ucStockImport.CreateBindingHeaderTable(itm.Text, PrimitivePropertyNames, i.ToString) 
         i += 1 
        Next 
       End If 
      Else 
       Throw New StockImportException("ucStockImport.BoundedObjects Is Nothing") 
      End If 
     End Sub 

     Private Shared Function CreateBindingHeaderTable(ByVal HeaderText As String, ByVal PropertyNames As List(Of String), ByVal ID As String) As String 
      Return String.Format("<table><tr><td>{0}</td></tr><tr><td>{1}</td></tr></table>", HeaderText, ucStockImport.CreateBindedObjectDropDownList(PropertyNames, ID)) 
     End Function 

     Private Shared Function CreateBindedObjectDropDownList(ByVal PropertyNames As List(Of String), ByVal ID As String) As String 
      Dim strBuilder As New StringBuilder 

      strBuilder.Append(String.Format("<option value=""{0}"">{1}</option>", i, propName)) 

      Dim i As Integer = 0 

      For Each propName As String In PropertyNames 
       strBuilder.Append(String.Format("<option value=""{0}"">", i) & propName & "</option>") 
       i += 1 
      Next 

      strBuilder.Append("</select>") 
      Return strBuilder.ToString 
     End Function 
+0

你可以發佈你的GenerateHeaderHTML方法的代碼嗎? – PhilPursglove 2010-07-28 08:40:17

+0

請在上面找到該代碼... :-) – 2010-07-28 11:53:10

0

你可以把一個DropDownList到一個GridView列很容易地通過使用TemplateColumn中:

<asp:GridView runat="server" ID="ComboboxGridView"> 
    <Columns> 
     <asp:TemplateField HeaderText="Column 1"> 
      <HeaderTemplate> 
       <asp:DropDownList runat="server" ID="Column1DropDownList" /> 
      </HeaderTemplate> 
      <ItemTemplate> 
       <asp:Label runat="server" ID="Column1DisplayLabel" Text='<%# Eval("Column1") %>' /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

,你可以很容易地將DropDownList到另一個數據源綁定,尤其是你正在使用的數據源控制。我不清楚你在標題中使用DropDownLists做什麼,但它是用於過濾出現在GridView中的行嗎?

+0

你的幫助是非常讚賞然而,這不會在我的情況是,我的數據源將包含列的大量工作是在設計時未知的,因此使用一個TemplateField將不解決我的問題。 想象一下我正在努力實現的一刻。我有一個包含多個庫存項目的csv文件。此外,我在我的應用程序中有一個名爲Stock的對象。我正在創建一個網格,允許用戶將股票文件的屬性綁定到我的基礎對象的屬性。 – 2010-07-28 06:57:26