index number of a datarow in a data table

Discussion in 'ASP .Net' started by TB, Jan 15, 2006.

  1. TB

    TB Guest

    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
     
    TB, Jan 15, 2006
    #1
    1. Advertising

  2. 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
    >
    >
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Jan 15, 2006
    #2
    1. Advertising

  3. TB

    TB Guest

    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
     
    TB, Jan 15, 2006
    #3
  4. 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.
    --
    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
    >
    >
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Jan 16, 2006
    #4
  5. TB

    TB Guest

    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
     
    TB, Jan 16, 2006
    #5
  6. =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Jan 16, 2006
    #6
  7. TB

    TB Guest

    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.
     
    TB, Jan 16, 2006
    #7
  8. TB

    nagpal.ritesh10

    Joined:
    Sep 2, 2010
    Messages:
    1
    you can get index of a datarow in datatable as follows:

    objDataTable.Rows.IndexOf(objDataRow)
     
    nagpal.ritesh10, Sep 2, 2010
    #8
  9. TB

    allAboutMe

    Joined:
    Jun 28, 2012
    Messages:
    1
    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: Jun 28, 2012
    allAboutMe, Jun 28, 2012
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug
    Replies:
    0
    Views:
    875
  2. Paul W
    Replies:
    1
    Views:
    1,322
    Bryant Hankins
    Aug 6, 2004
  3. debiken
    Replies:
    0
    Views:
    265
    debiken
    Nov 17, 2003
  4. debiken
    Replies:
    1
    Views:
    374
    Mike Moore [MSFT]
    Nov 18, 2003
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    307
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page