Newbie: Positions for BindingManager and its bound data-source: changing current item.

  • Thread starter Thread starter Ricardo Vazquez
  • Start date Start date
R

Ricardo Vazquez

My application has several text-box controls bound to a DataSet.
It's easy to navigate the data incrementing or decrementing the
BindingManager Position (first / previous / next / last --> easy, ok).
What do I want? I want to write on one of those text-box controls instead of
clicking prev/next buttons. I mean, if one of those boxes shows the
auto-numerical key of the current row (let's say it shows "1"), I want to be
able to write "14" and get the whole collection of data-bound-controls
showing the values of the row whose auto-numerical key is 14. Then write "7"
and get the whole collection of data-bound-controls showing the values of
the row whose key is 7. And so on.
To get this I use the "TextChangeEvent".
The code I think I would need for this event is something like this:
1- Find the row whose key is 14 (for the example) [DataTable.Rows.Find(14)]
2- Get its "Position" at the collection (let's say it's the 9th. element)
[a dataset is a collection controlled by BindingManager...]
3- Assign it to the BindingManager Position [BindingManagerBase.Position =
9]

So, my problem is with point 2:
How do I know the "Position" (in order to assign it to the BindingManager)
of the row that I am able to Find within the DataSet?
Or, is there another way to get the current item changed, so that the
CurrencyManager notifies the bound controls? (Because
"DataTable.Rows.Find(14)", wich gives me the DataRow I look for, does not
raise "current item changed").

Thank you!
 
I've already got this answer and it works wonderful:
You can bind to a DataView instead, having this view sorted by the key
values. After that, invoke the Find method and it will return the index of
the row found. This index can be used directly as the new Position value.

You have to bind both the form-controls and the BindingManager to a
DataView, instead of the DataSet created by the DataAdapter.
To populate the DataView I gave as the data-source in its constructor my
previous DataSet.

Important HINT:
I found this problem at first, which nearly makes me give up: Often when I
wrote the row key ("9", for example) on my textbox to get the whole
collection of data-bound-controls showing the several fields of that very
row (the row whose key field is 9), it happened:
1.- the data-bound-controls did not change what they were showing (the data
corresponding to the row that was shown before typing "9" was still on
them);
2.- the row whose key field is 9 gets CHANGED on the DataView!! (its
refilled with the data corresponding to the row that was shown before typing
"9")
After struggling for more than an hour I found this quite neat solution:
Before assigning the Position given by the Find method of the DataView to
the Position property of the BindingManager you have to invoke the
CancelCurrentEdit method of the BindingManager! And that's it!



Ricardo Vazquez said:
My application has several text-box controls bound to a DataSet.
It's easy to navigate the data incrementing or decrementing the
BindingManager Position (first / previous / next / last --> easy, ok).
What do I want? I want to write on one of those text-box controls instead of
clicking prev/next buttons. I mean, if one of those boxes shows the
auto-numerical key of the current row (let's say it shows "1"), I want to be
able to write "14" and get the whole collection of data-bound-controls
showing the values of the row whose auto-numerical key is 14. Then write "7"
and get the whole collection of data-bound-controls showing the values of
the row whose key is 7. And so on.
To get this I use the "TextChangeEvent".
The code I think I would need for this event is something like this:
1- Find the row whose key is 14 (for the example) [DataTable.Rows.Find(14)]
2- Get its "Position" at the collection (let's say it's the 9th. element)
[a dataset is a collection controlled by BindingManager...]
3- Assign it to the BindingManager Position [BindingManagerBase.Position =
9]

So, my problem is with point 2:
How do I know the "Position" (in order to assign it to the BindingManager)
of the row that I am able to Find within the DataSet?
Or, is there another way to get the current item changed, so that the
CurrencyManager notifies the bound controls? (Because
"DataTable.Rows.Find(14)", wich gives me the DataRow I look for, does not
raise "current item changed").

Thank you!
 
Back
Top