469,317 Members | 1,846 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,317 developers. It's quick & easy.

index number of a datarow in a data table

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

Jan 15 '06 #1
6 5473
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

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com

"TB" wrote:
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

Jan 15 '06 #2
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

Jan 15 '06 #3
You might be able to avoid searching for the row index if you bind the data
to server controls (which have index for selected records).

http://www.webswapp.com/CodeSamples/...FormView2.aspx

I put at this link a demo where I bound a dropdownlist and a FormView to do
the function that you are trying to do without writing codebehind to search
for the row index.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"TB" wrote:
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

Jan 16 '06 #4
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

Jan 16 '06 #5
In ver#1.1, you can use a datagrid with PageSize=1 to get the equivalent of a
FormView. The rest is the same. Here is a demo in VB:
http://www.societopia.net/samples/record_select.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"TB" wrote:
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

Jan 16 '06 #6
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.

Jan 17 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Sean C. | last post: by
11 posts views Thread by Tim Frawley | last post: by
1 post views Thread by Cordine | last post: by
4 posts views Thread by Brian Henry | last post: by
8 posts views Thread by Joe Rattz | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.