how to get the indexes of selected items in DataGrid?
-
The DataGrid support multi-selection operations. But when I have multi-selected some rows, How can I determine which rows have been selected?
Create a collection of some type (like an
ArrayList
), then enumerate the rows in yourDataGrid
. You can do this using theBindingContext
of theDataGrid
to get theCurrencyManager
which associates rows in theDataSource
to their displayed rows (since sorting or filtering may change index mapping). For each row index, you can callDataGrid.IsSelected
, passing the row index. Iftrue
, add to the collection of selected rows. The only real question is whether or not multiple rows are truly selected (i.e., they make look it but their state is a matter of theDataGrid
's implementation). I don't know why there isn't a much easier way (like with theListView
). Perhaps MS thought it wouldn't be oft-used enough to warrant the resources. Of course, this could be said about a lot of things in the .NET 1.x BCL! :mad:-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----