Easiest way to bind a grid datasource to a datatable

Discussion in 'ASP .Net' started by =?Utf-8?B?S2VubmV0aA==?=, Jan 18, 2004.

  1. Hi,

    I want to make sure I got the easiest way on how to bind a grid's datasource to a datatable.

    I've made an in-memory datatable

    Dim dtTotal As DataTable
    Dim drow As DataRow
    dtTotal = New DataTable("Total")
    ' Add two columns
    dcol = dtTotal.Columns.Add("Str1", System.Type.GetType("System.String"))
    dcol = dtTotal.Columns.Add("Dec1", System.Type.GetType("System.Decimal"))
    ' Add two rows
    drow = dtTotal.NewRow()
    drow("Str1") = "The first value"
    drow("Dec1") = 1001
    dtTotal.Rows.Add(drow)
    drow = dtTotal.NewRow()
    drow("Str1") = "The second value"
    drow("Dec1") = 1002
    dtTotal.Rows.Add(drow)

    As I understand, you only should need to create a dataview object and bind it to a grid's datasource

    Dim dv As DataView
    dv = dtTotal.DefaultView
    grid.DataSource = dv
    grid.DataBind()

    Is this the easiest way or the way with least resources on the server?

    /Kenneth
    =?Utf-8?B?S2VubmV0aA==?=, Jan 18, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?S2VubmV0aA==?=

    Teemu Keiski Guest

    Well,

    all I can say is that you don't necessarily need to create the DataView
    explicitly for databinding (binding happens to DataView, dt.DefaultView,
    automatically if you provide the DataTable as data source)

    e.g

    grid.DataSource = dtTotal
    grid.DataBind()

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist

    "Kenneth" <> wrote in message
    news:...
    > Hi,
    >
    > I want to make sure I got the easiest way on how to bind a grid's
    > datasource to a datatable.
    >
    > I've made an in-memory datatable
    >
    > Dim dtTotal As DataTable
    > Dim drow As DataRow
    > dtTotal = New DataTable("Total")
    > ' Add two columns
    > dcol = dtTotal.Columns.Add("Str1", System.Type.GetType("System.String"))
    > dcol = dtTotal.Columns.Add("Dec1", System.Type.GetType("System.Decimal"))
    > ' Add two rows
    > drow = dtTotal.NewRow()
    > drow("Str1") = "The first value"
    > drow("Dec1") = 1001
    > dtTotal.Rows.Add(drow)
    > drow = dtTotal.NewRow()
    > drow("Str1") = "The second value"
    > drow("Dec1") = 1002
    > dtTotal.Rows.Add(drow)
    >
    > As I understand, you only should need to create a dataview object and bind
    > it to a grid's datasource
    >
    > Dim dv As DataView
    > dv = dtTotal.DefaultView
    > grid.DataSource = dv
    > grid.DataBind()
    >
    > Is this the easiest way or the way with least resources on the server?
    >
    > /Kenneth
    Teemu Keiski, Jan 18, 2004
    #2
    1. Advertising

  3. Kiitos Teemu,

    /Kenneth
    =?Utf-8?B?S2VubmV0aA==?=, Jan 18, 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. J. Babe
    Replies:
    1
    Views:
    8,335
    Kevin Spencer
    Aug 15, 2003
  2. Fredrik Rodin

    Sort a datatable and create a new datatable

    Fredrik Rodin, Feb 16, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    9,041
    paul.mariotti
    Sep 28, 2007
  3. =?Utf-8?B?Qmx1ZWZsb3dlcg==?=

    Casting DataTable to class inherit from DataTable

    =?Utf-8?B?Qmx1ZWZsb3dlcg==?=, Apr 2, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    991
    =?Utf-8?B?Qmx1ZWZsb3dlcg==?=
    Apr 2, 2004
  4. Ersin Gençtürk
    Replies:
    7
    Views:
    25,473
    P-Dub
    Jun 28, 2011
  5. gnewsgroup
    Replies:
    2
    Views:
    471
    gnewsgroup
    Feb 3, 2008
Loading...

Share This Page