I would need to create a dynamic gridview, the number of columns are variable. I try to explain myself better ..
I have to create a table that contains a column for each hotel and in the rows the days of the month with the relative people booked for each hotel. EXAMPLE:
For each user I have saved in the db the hotels that he can view, so I have to create a datagridview with columns that are created dynamically. I would like to display a hyperlink in the number of people booked so that if you click on it, it opens a page with the details of the reservation, passing the data of the day and the hotel
Since the ID and DAY columns will always be there, I thought not to create them dynamically, but to write them in the code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AllowPaging="True" Width="100%" AllowSorting="True" class="table table-hover table-striped">
<AlternatingRowStyle BackColor="#DFDFDF" />
<PagerStyle ForeColor="#3a4f63" HorizontalAlign="Right" BackColor="#C6C3C6"></PagerStyle>
<Columns>
<asp:BoundField DataField="id" HeaderText="Id" InsertVisible="False" ReadOnly="True"
ShowHeader="False">
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="giorno" HeaderText="Giorno" InsertVisible="False" ReadOnly="True"
ShowHeader="False">
<ItemStyle Width="50px" />
</asp:BoundField>
</Columns>
</asp:GridView>
I initialize my DataTable dt with the first columns id and day
Dim dt As New DataTable
dt.Columns.Add("id")
dt.Columns.Add("day")
in the query that finds the hotels to display, I add the hyperlink columns to the datagridview
dt.Columns.Add(dr("hotel"))
Dim tfield As New HyperLinkField()
tfield.HeaderText = dr("hotel")
tfield.NavigateUrl = "info_booking.aspx?ID={0}"
tfield.Text = "11" 'IF I OMIT THE LINE, I DO NOT DISPLAY ANYTHING
GridView1.Columns.Add(tfield)
now I do a loop where I populate the lines with hotels and reservations and add the data to the DataTable as follows
...
dt.Rows.Add("1", "01/07/2021", "100", "90")
dt.Rows.Add("2", "02/07/2021", "102", "92")
...
GridView1.DataSource = dt
GridView1.DataBind()
I have the following problem, if I omit the line
tfield.Text = "11" 'IF I OMIT THE LINE, I DO NOT DISPLAY ANYTHING
the datagridview is composed as follows
If, on the other hand, I write the line and assign any value, for example 11, I display 11 on all lines, that is
I would like to understand how to modify the text and the parameters to be passed in the hyperlink of each single row and column.
I've changed hyperlink to bounfield and than I create a hyperlink in event RowDataBound. So I've deleted this code:
Dim tfield As New HyperLinkField()
tfield.HeaderText = dr("hotel")
tfield.NavigateUrl = "info_booking.aspx?ID={0}"
tfield.Text = "11" 'IF I OMIT THE LINE, I DO NOT DISPLAY ANYTHING
and write this code:
Dim bfield As BoundField = New BoundField()
bfield.HeaderText = dr("hotel")
bfield.DataField = "id_" & dr("hotel")
GridView1.Columns.Add(bfield)
than I add row in this hand:
Row("id") = "2"
Row("giorno") = "02/07/2021"
Row("id_Hotel 1") = "102"
Row("id_Hotel 2") = "103"
dt.Rows.Add(Row)
and I've create event datarowbound:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
For i = 0 To i_rif
Dim hp As HyperLink = New HyperLink()
hp.Text = e.Row.Cells(i + 2).Text
hp.Target = "_blank"
hp.NavigateUrl = "~/Dettaglio_rifugio_giorno.aspx?data=" & e.Row.Cells(1).Text & "&id_rifugio=" & arr_rif(i)
e.Row.Cells(i + 2).Controls.Add(hp)
Next
End If
End Sub