Datagridview how to discard a selected row in a custom object

I am using C # winforms with an MSSQL database. I have a table in the Pilots database, I populate the datagridview "dgvPilots", with data from the Pilots table.

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

I turn on multiselect. now i need to get multitask data from datagridview. How can I display multisecond strings for a Pilots object and get PilotsID.

My current error is: "Cannot use DataGridViewRow object type to enter" .Data.Pilots "...

I am also trying to do it like

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

but it returns the type of the DataGridViewRow element.

+4
source share
5 answers

DataBoundItem, .

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);
}

, ( , ).

MSDN DataBoundItem: http://msdn.microsoft.com/en-us/library/system.windows.forms. datagridviewrow.databounditem(v = vs .110).aspx

+14

, ,

var selectedPilots = dgvPilots.SelectedRows.Cast<Pilots>().ToList();

- . , , Pilots - DataTable, , Class. , , Pilot () , .

+5
List<int> indexes = DataGrid1.SelectedRows.Cast<DataGridViewRow>().Select(x => x.Index).ToList();

            foreach (i in indexes)
            {
                Pilots Pilot = (Pilots)DataGrid1.Rows[i].DataBoundItem;
            }
+1

- , :

    public static List<T> ToList<T>(this DataGridViewSelectedRowCollection rows)
    {
        var list = new List<T>();
        for (int i = 0; i < rows.Count; i++)
            list.Add((T)rows[i].DataBoundItem);
        return list;
    }

: dgvPilots.SelectedRows.ToList<Pilots>()

+1

First, wait three years for your answer.

Secondly, please do not repeat.

var selectedPilots = 
            (from DataGridViewRow cada in dgvPilots.SelectedRows select cada.DataBoundItem)
            .Select(x => (Pilots)x).ToList();
0
source

Source: https://habr.com/ru/post/1539585/


All Articles