Gridview 与标题中的组合框绑定

发布于 2024-09-11 17:02:48 字数 405 浏览 1 评论 0原文

谁能告诉我如何在 ASP.Net 4 中绑定到 gridview,其中 gridview 的第一行应该是标题,第二行应该是每列的组合框,第三行是开头我的实际数据源。

如果您可以想象我想要实现的目标是能够在数据网格中的每一列与另一个数据源之间创建绑定。该绑定是通过用户在组合框中选择一个值来创建的。然而,无论我如何尝试,我似乎都无法实现这一目标。

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

Could anyone please enlighten me about how one might go about binding to a gridview in ASP.Net 4 in a scenario where the first row of my gridview should be the headers, the second should be a combobox for each column and the third is the beginning of my actual datasource.

If you can imagine what I am trying to achieve is an ability to create a binding between each column in the datagrid and another datasource. This binding is created by the user selecting a value in the comboboxes. However no matter what I try I cant seem to achieve this.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独留℉清风醉 2024-09-18 17:02:48

您可以使用 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 中出现的行吗?

You can put a DropDownList into a Gridview column quite easily by using a 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>

and you can bind the DropDownList to another data source quite easily, especially you're using the DataSource controls. I'm not clear on what you're doing with the DropDownLists in the header though - is it for filtering the rows that appear in the GridView?

帅的被狗咬 2024-09-18 17:02:48

因此,对于任何好奇的人来说,这似乎是问题的解决方案。

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

So for anyone curious this appears to be the solution to the problem.

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: If anyone has any better solutions I would love to hear them :-)

The following is the code I have in the GenerateHeaderHTML(). My code is a very specific case (and prob far from great). However note that you can use any html you wish.

    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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文