index number of a datarow in a data table

T

TB

Hi All:

Here´s a very simple question:

I have created a datatable inside a dataset, and subsequently selected
a particular row using certain criteria. How do get the index number of
that particular row? There doesn´t seem to be property like
"mydatable.currentlyselectedrowindex" or "myrow.indexnumber".

Any suggestions will be highly appreciated.

TB
 
G

Guest

You probably should not build a program that depends on the row index.

There is not an index property for the DataRow because a program does not
usually depend on an index that would change every time the collection is
resorted or re-filtered. If, for whatever reason, you have to get the row
index you might have to iterate through the rows collection till you find the
a row that equals the row you have selected, for example:

Dim foundRow As DataRow
' Create an array for the key values to find.
Dim findTheseVals(2) As Object
' Set the values of the keys to find
findTheseVals(0) = "John"
findTheseVals(1) = "Smith"
findTheseVals(2) = "5 Main St."
foundRow = myTable.Rows.Find(findTheseVals)
Dim i As Integer
Dim iRowIndex As Integer
For i = 0 To myTable.Rows.Count - 1
If myTable.Rows(i).Equals(foundRow) Then
iRowIndex = i
Exit For
End If
Next
 
T

TB

Thanks for replying so fast.

You see, my requirements are the following:

I need to create a page which has two functions: 1) display the content
a specific record in a database table and 2) page forward and backward
from the initial specific record mentioned before.

When the page loads, a datatable ("mydatatable") is created, and a
specific record is selected according certain criteria. Furthermore,
the page contains "Next" and "Previous" buttons, which allows for the
user to move forward and backwards in the datatable, but always using
the initially selected record as a starting point.

My idea is therefore to identify the initially selected record by its
index number, from which one can more up and down by adding to or
subtracting from that initial value (the minimum value being 0 and the
maximum value being mydatatable.rows.count - 1)

Let me know what you think.

Thanks

TB
 
T

TB

You seem to use ASP.NET 2.0 for your demo. Since I am still in the 1.1x
world, I wonder whether your example will work. Furthermore, I "only"
understand VB.NET, and not C#.

Please bear with me.....

TB
 
T

TB

Thanks a lot for your insight.

Going back to your initial proposal, I wondered if it could simplified
a bit, because it seems like it runs through the entire mytable twice
(first at the "mytable.Rows.Find(findthesevals) and later in the
For.....Next loop. How about something like this (in this case inside a
page_load sub):

Dim foundRow As DataRow
Dim irecord as integer
Dim i As Integer
Dim iRowIndex As Integer

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then 'The first time the page is accessed
Session("irecord") = Request.QueryString("id") 'Just for the sake of
this example
For i = 0 To myTable.Rows.Count - 1
If myTable.Rows(i).item("ID") = irecord Then
foundrow = mytable.rows(i)
iRowIndex = i
Session("iRowIndex") = irowIndex
Exit For
End If
Next i
Else
irecord = Session("irecord")

End If
filltable() 'referrring to a sub where the table is populated
with foundrow content
End Sub

On the page there could be two buttons for navigating back and forward.
This would be the associated code:

Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrevious.Click
if irowIndex <> 0 then
Session("irowIndex") = Session("irowIndex") - 1
foundrow = mytable.Rows(Session("irowIndex"))
filltable()
end sub

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrevious.Click
if irowIndex <> (mytable.Rows.Count - 1) then
Session("irowIndex") = Session("irowIndex") + 1
foundrow = mytable.Rows(Session("irowIndex"))
filltable()
end sub


Please let me know what you think.
 
Joined
Jun 28, 2012
Messages
1
Reaction score
0
allAboutMe

Any body who tells you you don't need to be able to get the index of a DataRow or DataRowView lives in fairly land.

We need a method for DataViews. The Rows.IndexOf method won't work if you what the index for a DataRowView and its not part of the default view.

This has been a problem ever since the first version of ADO.Net. The dataTable Rows.IndexOf method wasn't introduced until .Net Framework Version 2.0 .

Come on Microsoft get real and make it simple. (Complexity & Dependencies aren't the part of the solution.)
 
Last edited:

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

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top