Index on DataTable ?

F

fniles

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.AcceptChanges()
 
K

Kerry Moorman

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
 
B

Brian Gideon

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.AcceptChanges()

Not really. One exception is the PK which is indexed.
 
F

fniles

Thank you
How can you create PK on the 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.AcceptChanges()

Not really. One exception is the PK which is indexed.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top