Changing the binding position to a specific row.

Answered Changing the binding position to a specific row.

  • Tuesday, May 08, 2012 4:31 PM
     
     

    I want to change the binding position in a datatable (in a dataset) to a specific row, independent from whatever it is bound to (grid etc). I have a reference to the typed DataRow in my dataset that I want to "focus" upon, but can't find a way to directly set the binding position to that row. The only way I've found is to loop all positions until I match my datarow by ID :-

          CurrencyManager cm = (CurrencyManager)BindingContext[myDataSet.myDataTable];

        cm.Position = 0;

        while (((DataRowView)cm.Current).Row["ID"].ToString() != myDataRowToSearchFor.ID.ToString()&& cm.Position < myDataSet.myDataTable.Rows.Count)

            cm.Position++;

    There must be an easier way surely! I have the typed DataRow in the dataset I want to make 'current', how can I do this directly without looping?


    • Edited by jezBo Tuesday, May 08, 2012 8:02 PM clarification
    •  

All Replies

  • Tuesday, May 08, 2012 4:38 PM
     
     
    I would suggest you to create a new DataTable, add columns (you can use Clone method) and then add rows you want to bind to controls.

    Mitja

  • Tuesday, May 08, 2012 4:40 PM
     
     
    I would suggest you to create a new DataTable, add columns (you can use Clone method) and then add rows you want to bind to controls.

    Mitja

    I don't think you have understood my question.
  • Tuesday, May 08, 2012 4:52 PM
     
     

    Ok, you have a datatable, which you use as a binding source to some controls, like textbox (as I would presume from the code).

    Now you have a DataRow reference (value) of a given row from DataTable. You would like to bind the row to this control based on this DataRow you got there?

    Am i close?


    Mitja

  • Tuesday, May 08, 2012 5:10 PM
     
     

    Ok, you have a datatable, which you use as a binding source to some controls, like textbox (as I would presume from the code).

    Now you have a DataRow reference (value) of a given row from DataTable. You would like to bind the row to this control based on this DataRow you got there?

    Am i close?


    Mitja

    I'm actually binding to a grid but I wanted to set the position within the dataset irrespective of what it is bound to. The issue is in setting the current position within the datasource without reference to screen items (so if it's bound to a grid you will see the focus move automatically), not binding the row to controls.

    • Edited by jezBo Tuesday, May 08, 2012 5:55 PM
    •  
  • Tuesday, May 08, 2012 6:58 PM
     
     

    In other words, given a row in my datasource "myDataTableRow", I want to focus on it so that everything bound to the datasource is updated accordingly : what I'd expect to be able to do is something like this:

    (CurrencyManager)BindingContext[myDataSet.myDataTable].Position = myDataTableRow.RowIndex;

    But RowIndex isn't a property of a typed DataRow. Hence my need for the loop, but I must be missing something simpler. I've searched these forums and scanned dozens of threads but they don't seem to provide the answer. I'd have thought it would be a common requirement.

  • Tuesday, May 08, 2012 8:38 PM
     
     Answered Has Code

    This could be a slightly better way, will this always work? :-

    CurrencyManager cm = (CurrencyManager)BindingContext[myDataSet.myDataTable];
    DataView dv = (DataView)cm.List;
    dv.Sort = "ID";
    int foundRowIndex = dv.Find(myDataTableRow.ID);
    if (foundRowIndex > -1)
        cm.Position = foundRowIndex;

    Would prefer it if I could do it without any scan/loop/find since I already have the row I want to focus on, but this is the best solution I can find.






    • Edited by jezBo Tuesday, May 08, 2012 8:39 PM
    • Edited by jezBo Tuesday, May 08, 2012 8:39 PM
    • Edited by jezBo Tuesday, May 08, 2012 8:40 PM
    • Marked As Answer by jezBo Wednesday, May 09, 2012 5:21 AM
    • Edited by jezBo Wednesday, May 09, 2012 5:22 AM
    • Edited by jezBo Wednesday, May 09, 2012 5:23 AM
    •