Creating DataGrid Via Code...

Discussion in 'ASP .Net Datagrid Control' started by Neo, Feb 21, 2004.

  1. Neo

    Neo Guest

    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
    Neo, Feb 21, 2004
    #1
    1. Advertising

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


    "Neo" <> wrote in message
    news:OOSZptB%...
    > 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
    >
    >
    Ken Cox [Microsoft MVP], Feb 21, 2004
    #2
    1. Advertising

  3. Neo

    Neo Guest

    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]" <> wrote in message
    news:%23ZMK0XC%...
    > 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>
    >
    >
    > "Neo" <> wrote in message
    > news:OOSZptB%...
    > > 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
    > >
    > >

    >
    Neo, Feb 22, 2004
    #3
  4. 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





    "Neo" <> wrote in message
    news:esKJywV%...
    > 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], Feb 22, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer
    Replies:
    1
    Views:
    5,017
    VB Programmer
    Aug 4, 2004
  2. =?Utf-8?B?SUdvdFlvdXJEb3ROZXQ=?=

    change datagrid headers via code

    =?Utf-8?B?SUdvdFlvdXJEb3ROZXQ=?=, Aug 13, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    7,819
    Shiva
    Aug 14, 2004
  3. RN
    Replies:
    1
    Views:
    416
  4. Gerry Hickman
    Replies:
    2
    Views:
    1,488
    Gerry Hickman
    Dec 14, 2005
  5. Elmar Baumann
    Replies:
    0
    Views:
    595
    Elmar Baumann
    Feb 2, 2004
Loading...

Share This Page