Populating DataGrid

Discussion in 'ASP .Net Web Controls' started by vbGansta, Jan 22, 2004.

  1. vbGansta

    vbGansta Guest

    Hi there!!

    Sorry for the simplicity of this one, but I am just getting my feet wet in
    this. How do I populate a data control manually from code? In other words
    I want to fill it myself not have it bound to a back end database. Is there
    another control that would better suit this? Any code examples would be
    awesome.

    Thanks,

    Joe
    vbGansta, Jan 22, 2004
    #1
    1. Advertising

  2. vbGansta

    ONIL@ Guest

    Hi vbGansta !

    You can use a DataTable like this :

    Dim dt As DataTable

    Dim dr As DataRow

    Dim i As Integer

    'create a DataTable

    dt = New DataTable

    dt.Columns.Add(New DataColumn("Name", GetType(String)))

    dt.Columns.Add(New DataColumn("First.", GetType(String)))

    dt.Columns.Add(New DataColumn("Last", GetType(String)))

    'Make the rows and add data.

    For i = 1 To 12

    dr = dt.NewRow()

    dr(0)= "Hi " & i+1
    dr(1) = "Hi" & i+2


    dr(2) = "Hi" & i+3

    'add each row to the DATATABLE

    dt.Rows.Add(dr)

    Next

    dr = dt.NewRow()

    dt.Rows.Add(dr)

    DataGrid1.DataSource = new DataView(dt)

    DataGrid1.DataBind

    I hope it helps
    Best Regards
    --
    ONil@.
    MX.




    --
    ONil@.
    MX.

    "vbGansta" <> wrote in message
    news:%...
    > Hi there!!
    >
    > Sorry for the simplicity of this one, but I am just getting my feet wet in
    > this. How do I populate a data control manually from code? In other

    words
    > I want to fill it myself not have it bound to a back end database. Is

    there
    > another control that would better suit this? Any code examples would be
    > awesome.
    >
    > Thanks,
    >
    > Joe
    >
    >
    ONIL@, Jan 23, 2004
    #2
    1. Advertising

  3. Here's some code that I use, adapted from the VS.NET documentation.

    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    If Not IsPostBack Then
    DataGrid1.DataSource = CreateDataSource()
    DataGrid1.DataBind()
    End If
    End Sub
    Function CreateDataSource() As ICollection
    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) = True
    dt.Rows.Add(dr)
    Next i
    Dim dv As New DataView(dt)
    Return dv
    End Function 'CreateDataSource

    "vbGansta" <> wrote in message
    news:%...
    > Hi there!!
    >
    > Sorry for the simplicity of this one, but I am just getting my feet wet in
    > this. How do I populate a data control manually from code? In other
    > words
    > I want to fill it myself not have it bound to a back end database. Is
    > there
    > another control that would better suit this? Any code examples would be
    > awesome.
    >
    > Thanks,
    >
    > Joe
    >
    >
    Ken Cox [Microsoft MVP], Jan 24, 2004
    #3
    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. John
    Replies:
    1
    Views:
    385
  2. Paul

    Timeout on populating a datagrid

    Paul, May 5, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    2,903
    Scott Allen
    May 5, 2004
  3. =?Utf-8?B?VGltOjouLg==?=

    Datagrid Dropdown not populating

    =?Utf-8?B?VGltOjouLg==?=, Aug 20, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    384
  4. Prime
    Replies:
    2
    Views:
    1,642
    Prime
    Oct 1, 2004
  5. Bob Hollness

    Populating a DataGrid

    Bob Hollness, Jan 12, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    2,497
    =?Utf-8?B?c2FyYQ==?=
    Jan 12, 2005
Loading...

Share This Page