How do you clear selected rows in DataGrid?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

There does not seem to be any way except a method called UnSelect which
requires an index- which I don't have.... is there any other way to simply
deselect rows or perhaps iterate through selected rows to get their index and
call unselect on them?
 
CurrencyManager cm =
(CurrencyManager)this.BindingContext[this.dgCharges.DataSource,
this.dgCharges.DataMember];
System.Data.DataView dv = (System.Data.DataView)cm.List;

for(int i=0; i<dv.Count; i++)
if(this.dgCharges.IsSelected(i))
this.dgCharges.UnSelect(i);
 
John J. Hughes II said:
CurrencyManager cm =
(CurrencyManager)this.BindingContext[this.dgCharges.DataSource,
this.dgCharges.DataMember];
System.Data.DataView dv = (System.Data.DataView)cm.List;

for(int i=0; i<dv.Count; i++)
if(this.dgCharges.IsSelected(i))
this.dgCharges.UnSelect(i);

Ouch- brute force !

I found another person who had the same problem and they used the
CurrentCellChanged event to put any cell indexes selected into an ArrayList
which can then be iterated through to perform some action on the selected
rows or to just UnSelect them all- looks like it works quite well.

Thanks for your reply!
 
MrNobody said:
John J. Hughes II said:
CurrencyManager cm =
(CurrencyManager)this.BindingContext[this.dgCharges.DataSource,
this.dgCharges.DataMember];
System.Data.DataView dv = (System.Data.DataView)cm.List;

for(int i=0; i<dv.Count; i++)
if(this.dgCharges.IsSelected(i))
this.dgCharges.UnSelect(i);

Ouch- brute force !

I found another person who had the same problem and they used the
CurrentCellChanged event to put any cell indexes selected into an
ArrayList
which can then be iterated through to perform some action on the selected
rows or to just UnSelect them all- looks like it works quite well.

Thanks for your reply!

Yea I like really big hammers :)

I assume you mean cell index by the index into the dataset of the record?
If not I have had problems with the cell number changing.

One problem I would have with putting the data into an array is that you
then need to do a search for each record. Whereas if you are searching on
an index it's quick it will still have to read X records before finding the
record you are looking for. So if you have 100 records tagged out of 500
you will be doing 100 searches times X records. I would have a tendency to
think that if X averaged 5 records per search you are still looking at 500
records. If 499 records are tagged then you are looking at 2495 records.
But on the other side if there is only 1 record tagged you are only looking
at 5 records.

Mostly it would be a semantics thing I think where one or the other would be
faster depending on the number of records in the dataset.

Regards,
John
 
Back
Top