Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Creating DataGrid Via Code...

Reply
Thread Tools

Creating DataGrid Via Code...

 
 
Neo
Guest
Posts: n/a
 
      02-21-2004
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


 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      02-21-2004
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>
<asplaceHolder id="PlaceHolder1" runat="server"></asplaceHolder></P>
</form>


"Neo" <(E-Mail Removed)> wrote in message
news:OOSZptB%(E-Mail Removed)...
> 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
>
>


 
Reply With Quote
 
 
 
 
Neo
Guest
Posts: n/a
 
      02-22-2004
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 [Microsoft MVP]" <(E-Mail Removed)> wrote in message
news:%23ZMK0XC%(E-Mail Removed)...
> 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>
> <asplaceHolder id="PlaceHolder1"

runat="server"></asplaceHolder></P>
> </form>
>
>
> "Neo" <(E-Mail Removed)> wrote in message
> news:OOSZptB%(E-Mail Removed)...
> > 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
> >
> >

>



 
Reply With Quote
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      02-22-2004
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/de...mberstopic.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





"Neo" <(E-Mail Removed)> wrote in message
news:esKJywV%(E-Mail Removed)...
> 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
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VS.NET, ASP.NET - using via Frontpage Ext, now via SHAREPOINT? Gerry Hickman ASP .Net 2 12-14-2005 09:25 PM
Detect if stdin is via keyboard or via < file? srbstar@sbcglobal.net C Programming 3 08-31-2005 11:20 AM
Cordless phone via ATA via RJ45 router with no provider possible? ChrisR VOIP 10 04-26-2005 06:55 PM
To all Gurus: How can I edit/update a DataGrid in a DataGrid (nested DataGrid)? Possible? Andreas Klemt ASP .Net Datagrid Control 0 10-08-2003 01:19 AM
Sending pictures via email Via OE6+Win XP - I no longer get prompt to "make all my pictures smaller" Ian Roberts Digital Photography 3 09-21-2003 04:57 PM



Advertisments