How to duplicate GridView columns

0

I have a gridView as follows:

<asp:GridView ID="grid" runat="server">               
            <Columns>
                <asp:BoundField HeaderText="aaa" />
                <asp:BoundField HeaderText="bbb" />
                <asp:BoundField HeaderText="ccc" />             
            </Columns>
        </asp:GridView>

It is required that there is the option to determine in a dropDownList the number of times that the columns should be duplicated, in case it is selected in the dropDownList, the number 1 should be duplicated 1 time, thus:

<asp:GridView ID="grid" runat="server">               
            <Columns>
                <asp:BoundField HeaderText="aaa" />
                <asp:BoundField HeaderText="bbb" />
                <asp:BoundField HeaderText="ccc" />
                <asp:BoundField HeaderText="aaa" />
                <asp:BoundField HeaderText="bbb" />
                <asp:BoundField HeaderText="ccc" />
            </Columns>
        </asp:GridView>

if it is selected in the dropDownList, the number 2 should be doubled twice, thus:

<asp:GridView ID="grid" runat="server">               
            <Columns>
                <asp:BoundField HeaderText="aaa" />
                <asp:BoundField HeaderText="bbb" />
                <asp:BoundField HeaderText="ccc" />
                <asp:BoundField HeaderText="aaa" />
                <asp:BoundField HeaderText="bbb" />
                <asp:BoundField HeaderText="ccc" />
                <asp:BoundField HeaderText="aaa" />
                <asp:BoundField HeaderText="bbb" />
                <asp:BoundField HeaderText="ccc" />
            </Columns>
        </asp:GridView>

I found a way to add a certain number of rows, I need to do the same for the columns. The rows can be added as follows:

   Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Dim dt As New DataTable()
            dt.Columns.AddRange(New DataColumn(0) {New DataColumn("aaa")})
            For j = 1 To 2
                dt.Rows.Add(j)
            Next
            ViewState("dt") = dt
            Me.BindGrid()
        End If
    End Sub

    Protected Sub BindGrid()
        grid.DataSource = TryCast(ViewState("dt"), DataTable)
        grid.DataBind()
    End Sub
    
asked by yulfredy rodriguez 01.09.2017 в 17:59
source

1 answer

0

I would do it in the code behind, the gridview puts the property autogeneratecolumns in false , in the event selectindexchanged of dropdown would add the columns of gridview depending on the selected value

    
answered by 01.09.2017 в 21:13