Asp.net Gridview与combobox的绑定';标题中的s

Asp.net Gridview与combobox的绑定';标题中的s,asp.net,vb.net,data-binding,gridview,web-controls,Asp.net,Vb.net,Data Binding,Gridview,Web Controls,如果我的gridview的第一行应该是标题,第二行应该是每个列的组合框,第三行是实际数据源的开始,那么有人能告诉我如何绑定到ASP.NET4中的gridview吗 如果您可以想象,我试图实现的是在datagrid中的每个列和另一个数据源之间创建绑定的能力。此绑定由用户在组合框中选择值创建。然而,无论我怎么努力,我似乎都无法做到这一点 HeaderText1 | HeaderText2 | HeaderText3 ComboBox1 | ComboBox2 | ComboBox3 Dat

如果我的gridview的第一行应该是标题,第二行应该是每个列的组合框,第三行是实际数据源的开始,那么有人能告诉我如何绑定到ASP.NET4中的gridview吗

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

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

通过使用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控件时。我不清楚你是如何处理标题中的下拉列表的——它是用来过滤GridView中显示的行的吗?

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

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
Private Sub grdMainGrid_RowCreated(ByVal sender作为对象,ByVal e作为System.Web.UI.WebControls.GridViewRowEventArgs)处理grdMainGrid.RowCreated
如果Me.BoundedObject不是空的,那么
如果e.Row.RowType=DataControlRowType.Header,则
Dim PrimitivePropertyNames As List(字符串的数量)=ParserHelper.GetPrimitivePropertyNames(Me.BoundedObjects.ToList)
尺寸i为整数=0
对于e.Row.Cells中的每个itm作为表格单元格
itm.Text=ucStockImport.CreateBindingHeaderTable(itm.Text,PrimitivePropertyNames,i.ToString)
i+=1
下一个
如果结束
其他的
抛出新的StockImportException(“ucStockImport.BoundedObjects为Nothing”)
如果结束
端接头
私有共享函数CreateBindingHeaderTable(ByVal HeaderText作为字符串,ByVal PropertyNames作为(字符串的)列表,ByVal ID作为字符串)作为字符串
返回String.Format(“{0}{1}”,HeaderText,ucStockImport.CreateBindedObjectDropDownList(PropertyNames,ID))
端函数
私有共享函数CreateBindedObjectDropDownList(ByVal PropertyNames作为列表(字符串),ByVal ID作为字符串)作为字符串
Dim strBuilder作为新的StringBuilder
追加(String.Format(“{1}”,i,propName))
尺寸i为整数=0
将每个propName作为PropertyNames中的字符串
strBuilder.Append(String.Format(“,i)&propName&”)
i+=1
下一个
strBuilder.Append(“”)
返回strBuilder.ToString
端函数

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