add datagrid dynamically

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
 
K

Ken Cox [Microsoft MVP]

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
 
E

Eliyahu Goldin

Bill,

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

Eliyahu
 
M

Manohar Kamath

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.
 
B

bill

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top