0
class Student
{
    public Id {get;set;}
    public Name {get;set;} 
}

List<Student> studentList = new List<Student>{st1,st2,st3,st4,st5};

DataGridView dgvStudentName:

(Check)       Id     Name
true          S01    Andrea
false         S02    Mark
true          S03    Selena
true          S04    Bob
false         S05    Mike

I tried to get selectList from studentList which has DataGridViewCheckBoxCell is true in dgvStudentName (checked by user) (Ex: st1,st3,st4).

I used LinQ to get what I want:

List<Student> selectList = dgvStudentName.Cast<DataGridViewRow>()
              .Where(x=>(bool)x.Cells[0].Value == true)
              .Select(x=>studentList.Where(q=>q.Id)==x.Cell[1].Value.ToString())
              ....

But it seems to be a stupid code. Could anyone help me?

Thank you in advanced!

Shai Nguyễn
  • 85
  • 1
  • 7
  • if you had a property on the `Student` class called say `Checked` (boolean) it could be bound to the checked control on the grid. then you linq would be much easier – jazb Oct 16 '18 at 03:42
  • the problem is, this is just a simple Example of my project. the Class "Student" is not able to add more property, and the `dgvStudentName` is checked by user. – Shai Nguyễn Oct 16 '18 at 03:54
  • ok, that's a pity. if you could add the property is would not matter that it was checked by the user (that's the whole point of the binding) – jazb Oct 16 '18 at 03:58
  • If possible, create a complete new class and with Boolean property in addition to Name and Id properties. And Convert List of Student to the List of new class and bind the list to the DataGridView. Another way is to create a new class inheriting from the Student class and add only one Boolean property to it and Bind the list of new class to the DataGridView. – Chetan Oct 16 '18 at 08:08

0 Answers0