【问题标题】:Gridview binding with combobox's in headerGridview 与组合框在标题中的绑定
【发布时间】:2010-07-27 05:12:50
【问题描述】:

在我的 gridview 的第一行应该是标题,第二行应该是每列的组合框和第三行的情况下,有人可以告诉我如何绑定到 ASP.Net 4 中的 gridview是我实际数据源的开始。

如果您可以想象我想要实现的是在数据网格中的每一列和另一个数据源之间创建绑定的能力。此绑定由用户在组合框中选择一个值创建。但是,无论我尝试什么,我似乎都无法做到这一点。

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

【问题讨论】:

    标签: asp.net vb.net data-binding gridview web-controls


    【解决方案1】:

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

    【讨论】:

    • 非常感谢您的帮助,但这在我的场景中不起作用,因为我的数据源将包含的列数量在设计时是未知的,因此使用模板字段不会解决我的问题。想象一下我正在努力实现的目标。我有一个包含许多库存项目的 csv 文件。此外,我的应用程序中有一个名为 Stock 的对象。我正在创建一个网格,允许用户将股票文件的属性绑定到我的基础对象的属性。
    【解决方案2】:

    所以对于任何好奇的人来说,这似乎是解决问题的方法。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多