代码之家  ›  专栏  ›  技术社区  ›  Maxim Gershkovich

标题中带有组合框的Gridview绑定

  •  0
  • Maxim Gershkovich  · 技术社区  · 14 年前

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

    HeaderText1 | HeaderText2 | HeaderText3
    ComboBox1   | ComboBox2   | ComboBox3 
    DataRow1    | DataRow1    | DataRow1 
    DataRow2    | DataRow2    | DataRow2 
    DataRow3    | DataRow3    | DataRow3
    
    2 回复  |  直到 13 年前
        1
  •  0
  •   PhilPursglove    14 年前

    通过使用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中出现的行的吗?

        2
  •  0
  •   Maxim Gershkovich    14 年前

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

    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
    

    附言:如果有人有更好的解决方案,我很乐意听他们说:-)

    下面是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