473,657 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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()
Sep 18 '08 #1
3 4888
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()
Sep 18 '08 #2
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.
Sep 18 '08 #3
Thank you
How can you create PK on the dataTable ?
"Brian Gideon" <br*********@ya hoo.comwrote in message
news:b1******** *************** ***********@b38 g2000prf.google groups.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.
Sep 18 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4834
by: Oliver Drobnik | last post by:
Hi, I have a datagrid bound to a datatable. Now if I sort the datagrid by clicking on one of the column heads I can no longer use the index in the datagrid to find the corresponding row in the datagrid. How can I find out the new (sorted) index if I know the row in the datatable? kind regards
7
3392
by: Jason Callas | last post by:
I have a process that manages a list of trades. Each trade is a separate row in the table. I blast the process with 10,000 new trades in order to stress test it and end up with the below listed exception when I call the NewRow method. The number of times it happens is NOT consistent (sometimes a few times out of 10,000 and sometimes 1000+ times). The app is written in VB and I am using SyncLock everywhere the table is accessed but I am sure it...
0
1705
by: Ryan | last post by:
Is there a way to get a row's DataView index from the underlying DataTable index. Example DataTable (sorted by key - 1st column) DataView (sorted by second column) Row 0 - A, 2 Row 0 - B, 1 Row 1 - B, 1 Row 1 - A, 2 Row 2 - C, 2 Row 2 - C, 2 I know the key of the row I want and can get the index of that row in the
4
1215
by: jensen bredal | last post by:
Hello, I have a Datatable "dt" with say "n" rows. I want to return a subset of "dt" starting at index "i" (i<dt.Rows.Count) and ending at index "j" ( dt.Rows.Count >j>i). How can this be done without unnecessary coding? Many thanks in advance. JB
5
1929
by: Richard | last post by:
Windows Forms, csharp, 20 rows in DataTable 14 shown in view after filter applied. For any of the 14 how do I do the datarow or something else thing to access the data in the row of the table behind the view selected on the datagrid. DataGrid only gives an index based on the 1-14 visible on the datagrid and I can't seem to update anything I see in the dataview quickwatch that I can update the underlying row. I could manually keep track...
2
8970
by: JohnR | last post by:
Sorry if this is has an obvious answer, but I can't find it... I am using a datatable.select to obtain the rows in a datatable that meet certain criteria. My question is this: for each of the datarows that are returned to me as a result of the datatable.select how can I determine their index in the datatable.rows collection? For those interested in why, it is because the index in the datatable is the same as the index of the same row...
1
1986
by: =?Utf-8?B?UnlhbiBBbmRydXM=?= | last post by:
Does the int index of the DataTable.Rows collection place a size limit on how large a datatable can be? If not then how do you index DataTable.Rows for sizes greater than what an int can handle?
0
1645
by: Ryan Liu | last post by:
I have a program works fine in .NET 1.1 and just recompiled in .NET 2.0 without any code change. Compiles OK, but there is an exeception when execute it. Then I remove PK, it works all fine again. Is this a known bug : PK on auto increase DataColumn in DataTable cause NullReferenceException? dcLineId = new DataColumn(LINEID_STR,
6
3194
by: fniles | last post by:
I am using VB.NET 2005 and Access database. My program uses a timer that kicks in every 1 min to read from a database and copy the dataset table to a datatable. This database is in a class called clsStat.vb. Then I pass this class to a thread. Each thread will call a function inside the clsStat.vb to do a "Select" of the datatable. Every day around the same time this function that does the select of the databale gets errors like so:...
0
8411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.