Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / January 2006

Tip: Looking for answers? Try searching our database.

index number of a datarow in a data table

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
TB - 15 Jan 2006 18:22 GMT
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
Phillip Williams - 15 Jan 2006 19:39 GMT
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

Signature

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

> Hi All:
>
[quoted text clipped - 8 lines]
>
> TB
TB - 15 Jan 2006 23:16 GMT
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
Phillip Williams - 16 Jan 2006 05:20 GMT
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/aspnet20/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.
Signature

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

> Thanks for replying so fast.
>
[quoted text clipped - 20 lines]
>
> TB
TB - 16 Jan 2006 18:29 GMT
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
Phillip Williams - 16 Jan 2006 21:00 GMT
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
Signature

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

> 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"
[quoted text clipped - 3 lines]
>
> TB
TB - 16 Jan 2006 23:57 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.