Finding row number in DataTable

  • Thread starter Thread starter J L
  • Start date Start date
J

J L

I have a DataTable. I want to find the row number (not return the row,
just find its index in the DataTable) based on a set of values for one
or more columns in the DataTable. All I have seen are the Select and
DataView functions but they return the row not the original index in
the DataTable (as I understand it).

TIA,
John
 
JL,

I never say anymore it does not exist.

However I never found the answer on your question.

Without doing a index for loop of course.

(The name is indexnumber by the way there are no rownumbers)

I hope this message helps anyway

Cor
 
This probably doesn't answer your question, but on the off-chance that you
have a DataView on this table with a sort order set, you can use the
DataView.Find() method and it returns a row index.

~~Bonnie
 
HI Bonnie,

Right, and if you don't set Sort and RowFilter of DataView it should return
you the row index as it exists in table.
I didn't try it though since I never needed it.
Why do you (J L) need it anyway?
 
Hi Miha,

No, it won't work if you don't have a Sort specified (you get an exception).
The find is attempting to find a value in the column specified by the Sort,
so the Sort property has to be set. That's why I said that it may not help J
L, depending on what he's doing. But I thought I'd throw it out there, just
in case he did have a sorted DataView.

~~Bonnie



Miha Markic said:
HI Bonnie,

Right, and if you don't set Sort and RowFilter of DataView it should return
you the row index as it exists in table.
I didn't try it though since I never needed it.
Why do you (J L) need it anyway?
--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

Bonnie Berent said:
This probably doesn't answer your question, but on the off-chance that you
have a DataView on this table with a sort order set, you can use the
DataView.Find() method and it returns a row index.

~~Bonnie
 
For those interested, here is how I have implemented code to return
the index from a datatable given a set of field name/value pairs:


Public Function FindRecord(ByVal dTable As DataTable, _
ByVal fieldList As Hashtable) As Integer
'
' searches the passed DataTable (dTale) using
' the fieldList HashTable. The key of the hash table
' is the field name and the value is the field value.
' If a record containing all field values is found,
' it returns the row number of that record. If it
' is not found it returns -1

If IsNothing(dTable) Then
Throw New System.Exception("No DataTable. Can not perform Find
Record operation.")
Return -1
End If
If dTable.Rows.Count = 0 Then
Throw New System.Exception("No data in the DataTable. " & _
"Can not perform the Find Record
operation")
Return -1
End If
If IsNothing(fieldList) Then
Throw New System.Exception("No field list. " & _
"Can not perform the Find Record
operation")
Return -1
End If
If fieldList.Count = 0 Then
Throw New System.Exception("The field list is empty. " & _
"Can not perform the Find Record
operation")
Return -1
End If

Dim i As Integer
Dim key As Object
Dim blnFound As Boolean
For i = 0 To dTable.Rows.Count - 1
blnFound = True
For Each key In fieldList.Keys
If dTable.Rows(i).Item(key) <> fieldList.Item(key) Then
blnFound = False
End If
Next
If blnFound Then
Return i
End If
Next
Return -1
End Function

BTW...if there are any suggestions to improve this, I would be very
grateful to hear them.

John
 
Hi Miha,
Thank you for your response. To answer why I need this capability...I
have (and have had since old Paradox days) a very strong aversion to
data bound controls. Too easy to get lost and have user behavior that
I did not anticipate or can control. So I have writen my own data
access layer. Also, perhaps atypical, I do not need the full power of
all ADO.Net provides but just the abiltity to do pretty much what I
used to do with DAO and recordsets. When developing the methods I
required, I determined early on that the DataTable object is the best
fo passing required information. I had looked at classes and hash
tables etc. but decided that I was just mimicking what the data table
already gave me.

So, long story longer, I fill da data table on my form load and use it
to fill text boxes which I associate to table fields via thier tag
property. I can navigate quite easily by keeping track of the current
row being used in the data table and moving forward or backward on
that row (or as Cor points out, index). My need arises when the user
types an item into one of the text boxes. At that point, I want to
determine if it (and possibly a combination of other fields on the
form) exist in the data table. If not, I clear the form and allow the
user to enter a new record which gets saved to the underlying database
and the form's data table repopluated.

Now, I know there will be many who say this is re-inventing the wheel
when data binding can be used...all I can say is that I guess I am a
control freak and I like having my methods and classes handle,
validate, populate, save, etc.

Sorry for the length of this response and hope it answers your
question. BTW, while I do have an ego in my code, I am also not above
plagerising a good idea...so any comments are definelty welcome.

John
 
John,
BTW...if there are any suggestions to improve this, I would be very
grateful to hear them.
In my opinion can you better use the defaultview or with its other name the
dataview.

That will save you a lot of time. You don't get using that the actual index
of your datatable, however in my opinion is for that not any need when you
use this class.
(It is standard in every datatable and than the name is defaultview)

http://msdn.microsoft.com/library/d...ef/html/frlrfsystemdatadataviewclasstopic.asp

I hope this helps somehow.

Cor
 
Back
Top