2010-07-27 33 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