Dataset Current Row Position

I am programatically creating a dataset with a table and need to know the current row number I am on before doing something else so I can get back to it. I have no controls bound to this dataset/table and would prefer to do everything in program. In the old days of recordsets I just used to get the bookmark of the current record and then go back to that bookmark. I have searched and can't find how to do this and I know it is probably really simple.

Thanks



Answer this question

Dataset Current Row Position

  • noelc

    Hello!

    If you have a bindingnavigator on your form, this will help:

    row = Me.CustBindingNavigator.BindingSource.Position

    Example:

    Me.CustDataSet.Tables("Customers").Rows(row).Item("CustName") = "Agent Smith"

    Me.CustTableAdapter.Update(Me.CustDataSet.Vevok)


  • Richard Bysouth

    Hello!

    I'am new in VB. I have the same problem. I understand that there is'nt currentrow concept. So, what is the good idea for this:

    There is'nt Datagrid on the form. The user click on a button. I want to modify the current record displayed on the form. Here's the button's click event:

    Me.CustDataSet.Tables("Customers").Rows( ).Item("CustName") = "Smith"

    Me.CustTableAdapter.Update(Me.CustDataSet.Customers)

    Thanks!


  • ftedin

    DataSets do not have a current row, because there is no cursor as it was in ADO. You referencing to the rows by index as in array.

  • RokitMan

    Thanks, that does help a great deal with what I'm tryying to do. Any other suggestions would also be appreciated.


  • Frisco

    Hi

    I'm a new-B in .net too and have the same problem. Can I ask if you got a solution or possible work around for it

    I do have a datagrid on my form and made use of this:

    .....dataset.Tables("TableName").Rows(DataGridName.CurrentRowIndex)...

    , but it only works if you don't sort the grid... this will happen though.

    I've tried DataGridName.CurrentCell.RowNumber aswell but the same thing happens.

    Any suggestions

    Thanx!


  • w1z44rdy

    DataTable (/DataSet) doesn't have a CurrentRow concept.

    If you want to refer back to some row while doing a foreach, you can simply save the DataRow pointer and use it later.

    DataRow rowToProcess;
    foreach( DataRow row in myTable.Rows )
    {
    if ( someCondition )
    rowToProcess = row;
    }

    DoSomethingWith( rowToProcess );

    If you are adding rows, and need to find the last row, one way is:
    rowToProcess = myTable.Rows[ myTable.Rows.Count - 1 ];

    There are other ways depending on what processing you are doing and which APIs you're using.

    --VV [MS]
    vascov@microsoft.com


  • WilliamW6488

    You may want to look at DataRowCollection.IndexOf(Datarow row) which is new for .NET 2.0.

    Given any row you can get it's position in the Rows collection by using myRow.Table.Rows.IndexOf(myRow). Asuming RowCollection hasn't been modfied via InsertAT or Removed rows then Table.Row[position] will give myRow back. This API is very efficient and uses a binary-search like alorithm to get the position of a row, so performance overhead should be fairly minimal.

    Kawarjit Bedi

    Program Manager ADO.NET

    kbedi@microsoft.com



  • hcoded

    Hi

    Thanks for your reply, it works. I though do not always make use of bound controls, since there are too many processes running in the background. I made a small dummy app just to test and it worked fine, so I will try doing it the same way from code. Thanks again!


  • Ivan Giugni

    Thanks everyone for the replies. I have to get my head around a new mindset and try not to think in the previous versions of VB.


  • Dataset Current Row Position