Index on DataTable ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fniles

    Index on DataTable ?

    Can I create an index on a DataTable ?
    On the following DataTable, I would like to create index on "Price", can I
    do that ?
    Thank you.

    Dim DT As New DataTable

    DT.Columns.Add( "Price", GetType(System. Int32))
    DT.Columns.Add( "ID", GetType(System. Int32))
    DT.Columns.Add( "Vol", GetType(System. Int32))

    Dim r As DataRow = DT.NewRow
    r.Item(0) = 99
    r.Item(1) = 1234
    r.Item(2) = 350
    r.Item(3) = 1
    DT.Rows.Add(r)

    r = DT.NewRow
    r.Item(0) = 98
    r.Item(1) = 1233
    r.Item(2) = 300
    r.Item(3) = 1
    DT.Rows.Add(r)

    r = DT.NewRow
    r.Item(0) = 97
    r.Item(1) = 1230
    r.Item(2) = 375
    r.Item(3) = 1
    DT.Rows.Add(r)

    r = DT.NewRow
    r.Item(0) = 96
    r.Item(1) = 1231
    r.Item(2) = 250
    r.Item(3) = 1
    DT.Rows.Add(r)
    DT.AcceptChange s()


  • =?Utf-8?B?S2VycnkgTW9vcm1hbg==?=

    #2
    RE: Index on DataTable ?

    fniles,

    No, I don't believe you can add an index to a datatable.

    Keep in mind that datasets/datatables are not the same as an in-memory sql
    database and trying to treat them as such will only lead to frustration.

    Having said that, if you need an index for performance while selecting, you
    might look into the dataview and its find methods. It will index on whatever
    you set as its sort column.

    Kerry Moorman

    "fniles" wrote:
    Can I create an index on a DataTable ?
    On the following DataTable, I would like to create index on "Price", can I
    do that ?
    Thank you.
    >
    Dim DT As New DataTable
    >
    DT.Columns.Add( "Price", GetType(System. Int32))
    DT.Columns.Add( "ID", GetType(System. Int32))
    DT.Columns.Add( "Vol", GetType(System. Int32))
    >
    Dim r As DataRow = DT.NewRow
    r.Item(0) = 99
    r.Item(1) = 1234
    r.Item(2) = 350
    r.Item(3) = 1
    DT.Rows.Add(r)
    >
    r = DT.NewRow
    r.Item(0) = 98
    r.Item(1) = 1233
    r.Item(2) = 300
    r.Item(3) = 1
    DT.Rows.Add(r)
    >
    r = DT.NewRow
    r.Item(0) = 97
    r.Item(1) = 1230
    r.Item(2) = 375
    r.Item(3) = 1
    DT.Rows.Add(r)
    >
    r = DT.NewRow
    r.Item(0) = 96
    r.Item(1) = 1231
    r.Item(2) = 250
    r.Item(3) = 1
    DT.Rows.Add(r)
    DT.AcceptChange s()
    >
    >
    >

    Comment

    • Brian Gideon

      #3
      Re: Index on DataTable ?

      On Sep 18, 8:41 am, "fniles" <fni...@pfmail. comwrote:
      Can I create an index on a DataTable ?
      On the following DataTable, I would like to create index on "Price", can I
      do that ?
      Thank you.
      >
      Dim DT As New DataTable
      >
      DT.Columns.Add( "Price", GetType(System. Int32))
      DT.Columns.Add( "ID", GetType(System. Int32))
      DT.Columns.Add( "Vol", GetType(System. Int32))
      >
      Dim r As DataRow = DT.NewRow
      r.Item(0) = 99
      r.Item(1) = 1234
      r.Item(2) = 350
      r.Item(3) = 1
      DT.Rows.Add(r)
      >
      r = DT.NewRow
      r.Item(0) = 98
      r.Item(1) = 1233
      r.Item(2) = 300
      r.Item(3) = 1
      DT.Rows.Add(r)
      >
      r = DT.NewRow
      r.Item(0) = 97
      r.Item(1) = 1230
      r.Item(2) = 375
      r.Item(3) = 1
      DT.Rows.Add(r)
      >
      r = DT.NewRow
      r.Item(0) = 96
      r.Item(1) = 1231
      r.Item(2) = 250
      r.Item(3) = 1
      DT.Rows.Add(r)
      DT.AcceptChange s()
      Not really. One exception is the PK which is indexed.

      Comment

      • fniles

        #4
        Re: Index on DataTable ?

        Thank you
        How can you create PK on the dataTable ?


        "Brian Gideon" <briangideon@ya hoo.comwrote in message
        news:b1a45494-8189-4182-b01c-4a1e00309bff@b3 8g2000prf.googl egroups.com...
        On Sep 18, 8:41 am, "fniles" <fni...@pfmail. comwrote:
        Can I create an index on a DataTable ?
        On the following DataTable, I would like to create index on "Price", can I
        do that ?
        Thank you.
        >
        Dim DT As New DataTable
        >
        DT.Columns.Add( "Price", GetType(System. Int32))
        DT.Columns.Add( "ID", GetType(System. Int32))
        DT.Columns.Add( "Vol", GetType(System. Int32))
        >
        Dim r As DataRow = DT.NewRow
        r.Item(0) = 99
        r.Item(1) = 1234
        r.Item(2) = 350
        r.Item(3) = 1
        DT.Rows.Add(r)
        >
        r = DT.NewRow
        r.Item(0) = 98
        r.Item(1) = 1233
        r.Item(2) = 300
        r.Item(3) = 1
        DT.Rows.Add(r)
        >
        r = DT.NewRow
        r.Item(0) = 97
        r.Item(1) = 1230
        r.Item(2) = 375
        r.Item(3) = 1
        DT.Rows.Add(r)
        >
        r = DT.NewRow
        r.Item(0) = 96
        r.Item(1) = 1231
        r.Item(2) = 250
        r.Item(3) = 1
        DT.Rows.Add(r)
        DT.AcceptChange s()
        Not really. One exception is the PK which is indexed.


        Comment

        Working...