views:

46

answers:

2

Hi all,

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

A: 

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?

PhilPursglove
Your help is much appreciated however this will not work in my scenario as the amount of columns that my datasource will contain is unknown at design time and therefore using a templatefield will not solve my problem.Imagine what I am trying to achieve for a moment. I have a csv file that contains a number of stock items. Further I have an object in my application called Stock. I am creating a grid that allows the user to bind the properties of the stock file to the properties of my underlying object.
Maxim Gershkovich
A: 

Hi all,

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
Maxim Gershkovich
Can you post the code of your GenerateHeaderHTML method?
PhilPursglove
Please find the code above... :-)
Maxim Gershkovich