Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# - Multiple Select on Datagridview using BindingSource

Tags:

c#

I know that to get single selected row on a datagridview using a BindingSource is this code:

BindingSource.Current

My questions is how to get the multiple current selected on datagridview using a BindingSource

like image 204
Newboy11 Avatar asked Mar 14 '26 20:03

Newboy11


1 Answers

Guess it might be too late, but here is one solution...

If the DataGridView is bounded to a BindingSource and/or a List of Object, you might retrieve a List of Object accordind to the selected rows this way:

var myListOfYOURBJECTTYPE = dataGridViewEstados.Rows
                                        .Cast<DataGridViewRow>()
                                        .Where(r => r.Selected == true)
                                        .Select(d => (YOURBJECTTYPE)d.DataBoundItem)
                                        ToList();

...or:

var myListOfYOURBJECTTYPE = dataGridViewEstados.SelectedRows
                                        .Cast<DataGridViewRow>()
                                        .Select(d => (YOURBJECTTYPE)d.DataBoundItem)
                                        ToList();

You can't forget to configure your DataGridView SelectionMode to FullRowSelect!

like image 150
Ricardo Rodrigues Avatar answered Mar 16 '26 10:03

Ricardo Rodrigues



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!