How do I force a datatable to a position based upon an index key from another unbound control

L

Larry Woods

I have a combobox with a value field that represents a key in a dataset
table that is NO bound to this combobox. In the SelectedIndexChanged event
procedure I want to position the dataset table to the record with the key
from the combobox. I can't quite figure out the right combination of
methods to do this.

TIA,

Larry Woods
 
C

Cor Ligthert

Larry,

Just loop throught the datatable with a for index and set the
currencymanager to that when it is found just by comparing it and than exit
directly the loop.

(Do not be afraid of a loop it consumes moslty very few time and a method
will probably do the same under the hood, while when you do it yourself you
can control it).

I hope this helps?

Cor
 
M

Miha Markic [MVP C#]

Larry,

In SelectedIndexChanged event retrieve the key value from combobox.
Next, you will need to know what your datasource is - is it a DataTable or
DataView.
In the former case, DataTable.DefaultView DataView is used while in the
later DataView is directly used.
Once you know the source DataView, you can use a loop on this DataView,
something like Cor suggested on DataTable (his sample will work only if the
DefaultView isn't sorted or filtered).
 
C

Cor Ligthert

Miha,

The question was datatable, where is the datasource comming in (it is when I
read it right a table not related to the combobox except that the combobox
datasource has the same items?) And than I think what I wrote will work in
all kind of situations, or do I miss something?

:)

Cor

Miha Markic said:
Larry,

In SelectedIndexChanged event retrieve the key value from combobox.
Next, you will need to know what your datasource is - is it a DataTable or
DataView.
In the former case, DataTable.DefaultView DataView is used while in the
later DataView is directly used.
Once you know the source DataView, you can use a loop on this DataView,
something like Cor suggested on DataTable (his sample will work only if
the DefaultView isn't sorted or filtered).

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
SLODUG - Slovene Developer Users Group
www.rthand.com

Larry Woods said:
I have a combobox with a value field that represents a key in a dataset
table that is NO bound to this combobox. In the SelectedIndexChanged
event
procedure I want to position the dataset table to the record with the key
from the combobox. I can't quite figure out the right combination of
methods to do this.

TIA,

Larry Woods
 
M

Miha Markic [MVP C#]

Hi Cor,

Cor Ligthert said:
Miha,

The question was datatable, where is the datasource comming in (it is when
I read it right a table not related to the combobox except that the
combobox datasource has the same items?) And than I think what I wrote
will work in all kind of situations, or do I miss something?

You miss the fact that DataTable is never used as a datasource. Instead,
implicitly, DataTable.DefaultView is used.
To make things a bit more complicated, if sort is applied to the datasource,
the DataView row position wont match DataTable row position anymore. The
same goes for filtering. So, you can't just get index out of table and use
it for setting CurrencyManager blindly (ok, you can, if you are sure you
aren't doing sorting or filtering).
Your sample will work unless sorting or filtering is applied.
Hope this clears the situation.
 
L

Larry Woods

I found the solution, but for the life of me I can't explain it....

I created a listbox that ONLY has it's display member defined. The display
member is a NON-KEY field in the table. Then I have a set of textboxes that
are bound to all of the fields of the same table. When I click on an entry
in the listbox the textboxes are filled with the field values from the row
which contains the NON-KEY listbox member value!!! I don't see how the
listbox "binds" to the table since I don't have either the MemberValue NOR
and binding defined for the listbox.

Any ideas?

TIA,

Larry Woods
 
C

Cor Ligthert

Larry,

Than you have probably the textboxes binded to that table.

You have binded (using the datasource the datatable) to the same table.

The binding uses the currencymanager.

When you change in the currentrow in the listbox than that is set as well in
all the textboxes. It has nothing to do with whatever key.

What you can do as well by instance is
DirectCast(BindingContext(ComboBox1.DataSource), _
CurrencyManager).Position += 1

And you will see that everything show the next row in the datasource from
the combobox including the combobox itself.

I hope this explains it a little bit?

Cor

"Larry Woods"
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top