Datagridview how to cast selected row to custom object

Srđan Marković

I use c# winforms with MSSQL database. I have table in database "Pilots" , i fill datagridview "dgvPilots", with data from Pilots table.

dgvPilots.DataSource = Connection.dm.Pilots.ToList();

I enable multiselect. now i need to get multiselected data from datagridview. How can i multiselected rows cast to "Pilots" object and get PilotsID.

My current error is "Unable to cast object type DataGridViewRow to type ".Data.Pilots"...

i also try casting like this

dgvPilots.SelectedRows.Cast<Pilots>().ToList();

but it return DataGridViewRow item type.

Peter

You will need to iterate the collection and go after the DataBoundItem property which is the underlying data.

var pilots = new List<Pilots>(grid.SelectedRows.Count);

for(int index = 0; index < grid.SelectedRows.Count; index++)
{
   var selectedRow = grid.SelectedRows[index];
   var pilot = (Pilots)selectedRow.DataBoundItem;

   pilots.Add(pilot);
}

The code above shows how you can achieve this, (I freehanded the code so forgive any syntax errors).

Here is the msdn article on the DataBoundItem property: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.databounditem(v=vs.110).aspx

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Datagridview how to cast selected row to custom object

From Dev

C# Devexpress: Cast selected row to custom object

From Dev

Retrieving selected row in dataGridView as an object

From Dev

Retrieving selected row in dataGridView as an object

From Dev

DataGridView- How to jump to the selected row in search?

From Dev

How to check if have any row selected on DataGridView?

From Dev

how to delete selected row from datagridview

From Dev

How to delete selected row from datagridview and database?

From Dev

How to get the selected object from the DataGridView?

From Dev

Scroll to Datagridview selected Row

From Dev

Scroll to Datagridview selected Row

From Dev

How to delete last row in DataGridView and keep the row selected?

From Dev

How to cast an custom object to List<object>

From Dev

How to change row color in datagridview after checkbox is selected

From Dev

How to update selected row in datagridview through textbox located on another form?

From Dev

DatagridView: how to programmatically move the current index to the programmatically selected row?

From Dev

How to get selected row's all column values of dataGridView in TextBoxes

From Dev

How to add a new row to Winforms DataGridView in my custom column

From Dev

How to cast a Newton.Json deserialized generic object to a custom object?

From Dev

How to have custom control in DataGridView display object's value?

From Dev

vb.net datagridview not showing selected row

From Dev

DataGridView selected row to display in text boxes

From Dev

print selected row from datagridview Error

From Dev

NSTableRowView/NSTableCellView how to set custom color to selected row?

From Dev

Custom DataGridView returns object with columns

From Dev

Cast class to object with custom classloader

From Dev

DataGridView row text disappears on every row except the selected one

From Dev

DataGridView row text disappears on every row except the selected one

From Dev

When selecting all cells in a row of datagridview, set the row to selected as property

Related Related

  1. 1

    Datagridview how to cast selected row to custom object

  2. 2

    C# Devexpress: Cast selected row to custom object

  3. 3

    Retrieving selected row in dataGridView as an object

  4. 4

    Retrieving selected row in dataGridView as an object

  5. 5

    DataGridView- How to jump to the selected row in search?

  6. 6

    How to check if have any row selected on DataGridView?

  7. 7

    how to delete selected row from datagridview

  8. 8

    How to delete selected row from datagridview and database?

  9. 9

    How to get the selected object from the DataGridView?

  10. 10

    Scroll to Datagridview selected Row

  11. 11

    Scroll to Datagridview selected Row

  12. 12

    How to delete last row in DataGridView and keep the row selected?

  13. 13

    How to cast an custom object to List<object>

  14. 14

    How to change row color in datagridview after checkbox is selected

  15. 15

    How to update selected row in datagridview through textbox located on another form?

  16. 16

    DatagridView: how to programmatically move the current index to the programmatically selected row?

  17. 17

    How to get selected row's all column values of dataGridView in TextBoxes

  18. 18

    How to add a new row to Winforms DataGridView in my custom column

  19. 19

    How to cast a Newton.Json deserialized generic object to a custom object?

  20. 20

    How to have custom control in DataGridView display object's value?

  21. 21

    vb.net datagridview not showing selected row

  22. 22

    DataGridView selected row to display in text boxes

  23. 23

    print selected row from datagridview Error

  24. 24

    NSTableRowView/NSTableCellView how to set custom color to selected row?

  25. 25

    Custom DataGridView returns object with columns

  26. 26

    Cast class to object with custom classloader

  27. 27

    DataGridView row text disappears on every row except the selected one

  28. 28

    DataGridView row text disappears on every row except the selected one

  29. 29

    When selecting all cells in a row of datagridview, set the row to selected as property

HotTag

Archive