add datagrid dynamically

  • Thread starter Thread starter bill
  • Start date Start date
B

bill

Can I add multiple datagrid controls dynamically at runtime?

I want to break up my dataset into separate datagrids, so I can insert a "<p
style="page-break-before: always">
tag between them.

The objective is for the user to be able to print the data with pagebreaks
and column headers on each printed page.

Thanks
Bill
 
Hi Bill,

Yes, you can do exactly what you want to create datagrids at runtime. Here's
a little demo that should get you started. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


<form id="Form1" method="post" runat="server">
<P>
<asp:Label id="Label1" runat="server">No. of Grids: </asp:Label>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>
<P>
<asp:Button id="Button1" runat="server" Text="OK"></asp:Button></P>
<P>
<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder></P>
</form>

Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim dg As DataGrid
Dim intCounter As Integer
For intCounter = 0 To _
Convert.ToInt32(TextBox1.Text) - 1
dg = New DataGrid
dg.DataSource = CreateDataSource()
dg.BackColor = _
Color.FromArgb((intCounter * 60), _
intCounter * 20, 255)
dg.DataBind()
PlaceHolder1.Controls.Add(dg)
Next
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
 
Bill,

Consider using a repeater with a datagrid in the ItemTemplate and a
pagebreak in the Separator Template.

Eliyahu
 
You could add a control dynamically in Page's load method, as follows:

Page.Controls.Add(myGrid)

The control will be added to the end of the page. If you don't want this to
happen, but wish to add a control at a specific point, you have to create a
PlaceHolder control, and add to its controls collection.


in Page load:

PlaceHolder.Controls.Add(myGrid)


and in the page itself:

<asp:PlaceHolder runat="server" id="myPlaceHolder"/>

Hope that helps.
 
Now I see how easy it is to add a datagrid dynamically, although the page
break doesn't work.

I added a placeholder control to a panel control - otherwise the new
datagrid was superimposed over the other controls on the page. This works
great.

I then added a literal control with the "<p style='page-break-before:
always'></p>" to the placeholder.

In view-source the page break is in the correct place, but no page break
occurs during printing.

Can I add a page break to a placeholder like this?

Thanks
-bill



Ken Cox said:
Hi Bill,

Yes, you can do exactly what you want to create datagrids at runtime. Here's
a little demo that should get you started. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


<form id="Form1" method="post" runat="server">
<P>
<asp:Label id="Label1" runat="server">No. of Grids: </asp:Label>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>
<P>
<asp:Button id="Button1" runat="server" Text="OK"></asp:Button></P>
<P>
<asp:PlaceHolder id="PlaceHolder1"
 
Back
Top