在我的gridview的第一行应该是标题,第二行应该是每列的组合框和第三行的情况下,有谁可以请教我如何在ASP.Net 4中绑定到gridview。是我实际数据源的开始。
如果你能想象我想要实现的是能够在datagrid中的每一列和另一个数据源之间创建绑定。该绑定由用户在组合框中选择值来创建。然而,无论我尝试什么,我似乎无法实现这一点。
HeaderText1 | HeaderText2 | HeaderText3
ComboBox1 | ComboBox2 | ComboBox3
DataRow1 | DataRow1 | DataRow1
DataRow2 | DataRow2 | DataRow2
DataRow3 | DataRow3 | DataRow3
答案 0 :(得分:0)
使用TemplateColumn可以非常轻松地将DropDownList放入Gridview列中:
<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绑定到另一个数据源,尤其是您正在使用DataSource控件。我不清楚你在标题中使用DropDownLists做了什么 - 它是否用于过滤GridView中出现的行?
答案 1 :(得分: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