'c# - Multiple Select on Datagridview using BindingSource
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
Solution 1:[1]
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!
Solution 2:[2]
DataGridViewRow.DataBoundItem contains the 'business' object it is bound to. This sample:
DataRow row = (dataGridView1.SelectedRows[0].DataBoundItem as DataRowView).Row;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ricardo Rodrigues |
| Solution 2 | Tomato32 |
