Creating DataGrid Via Code...

N

Neo

Hey all,

Been reading up on ASP.NET (using VB.NET) and have been trying to figure out
if this is possible.

I need to make 4 connections to 4 different oracle databases and retreive
data from some tables there. Is it possible to create 4 datagrids to
display the data via code instead of having 4 datagrids hardcoded in the
code. The number of databases could change so programatically created them
would work best. I would think something in loop of some sort. for every
group of data returned, create a new datagrid and display the data.

Any thoughts welcomed..

James
 
K

Ken Cox [Microsoft MVP]

Hi James,

What you want to do is add a PlaceHolder to your page and then create as
many datagrids as you need using code. As you make one, add it to the
PlaceHolder's controls collection. The code below shows how it can be done.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto



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


<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>
 
N

Neo

Very Nice Ken... Worked perfectly in my situation. Another question for
you if you have a spare minute...

How would one set the width of the datagrid via code.

I've been trying this (well, I did it once :)

With dg
.Width = "50%"
.other prop
.other prop
End With

Is there some website or book that shows how to set all the datagrids
properties via code instead of in the designer? I think it's possible. I
just don't have the knowledge and cant' find anything on the inet that goes
indepth into this subject.

Thanks again all. It's been a long time since I've visited these groups
(changed line of work, but have changed back now). Forgot how cool it was
to be able to ask the world a question and get a flood of responses :)

Peace,

James


Ken Cox said:
Hi James,

What you want to do is add a PlaceHolder to your page and then create as
many datagrids as you need using code. As you make one, add it to the
PlaceHolder's controls collection. The code below shows how it can be done.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto



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


<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"
 
K

Ken Cox [Microsoft MVP]

Hi James,

You can set all the properties in code, including the width of a column.
Here's a sample, stolen mainly from my colleague Bipin Joshi here:
http://www.dotnetbips.com/displayarticle.aspx?id=66

By looking at the properties in the documentation, you can see what else you
can set:

http://msdn.microsoft.com/library/d...ystemwebuiwebcontrolsdatagridmemberstopic.asp

Ken
MVP [ASP.NET]
Toronto

Dim dt As DataTable
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
CreateGrid()
End Sub
Public Sub CreateGrid()
'declare a new datagrid and set properties
Dim DataGrid1 As New DataGrid
DataGrid1.BorderWidth = Unit.Pixel(2)
DataGrid1.CellPadding = 10
DataGrid1.GridLines = GridLines.Both
DataGrid1.BorderColor = Color.Blue
DataGrid1.ShowHeader = True
DataGrid1.AutoGenerateColumns = False
DataGrid1.SelectedItemStyle.BackColor = Color.Yellow
'add bound columns to the datagrid
Dim datagridcol As New BoundColumn
datagridcol.HeaderText = "Course"
datagridcol.ItemStyle.Width = Unit.Pixel(265)
datagridcol.DataField = "course"
DataGrid1.Columns.Add(datagridcol)
datagridcol = New BoundColumn
datagridcol.HeaderText = "First Name"
datagridcol.ItemStyle.Width = Unit.Pixel(100)
datagridcol.DataField = "firstname"
DataGrid1.Columns.Add(datagridcol)
Dim selectcol As New ButtonColumn
selectcol.ButtonType = ButtonColumnType.LinkButton
selectcol.Text = "Select"
selectcol.CommandName = "Select"
DataGrid1.Columns.Add(selectcol)
'add event handlers
AddHandler DataGrid1.SelectedIndexChanged, _
AddressOf DataGrid1_SelectedIndexChanged
'bind datagrid
DataGrid1.DataSource = GetDataSet()
DataGrid1.DataBind()
'add datagrid to the page
Page.Controls(1).Controls.Add(DataGrid1)
End Sub
Public Sub DataGrid1_SelectedIndexChanged _
(ByVal sender As Object, _
ByVal args As EventArgs)
'write your code here
End Sub

Function GetDataSet() As ICollection
' Create sample data for the DataList control.
dt = New DataTable
Dim dr As DataRow
' Define the columns of the table.
dt.Columns.Add(New DataColumn("firstname", GetType(String)))
dt.Columns.Add(New DataColumn("course", GetType(String)))

' Populate the table with sample values.
dr = dt.NewRow
dr(0) = "Ben"
dr(1) = "English"
dt.Rows.Add(dr)
dr = dt.NewRow
dr(0) = "Ben"
dr(1) = "Geology"
dt.Rows.Add(dr)
dr = dt.NewRow
dr(0) = "Ben"
dr(1) = "Physics"
dt.Rows.Add(dr)
Dim dv As DataView = New DataView(dt)
Return dv
End Function
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top