Ivan Climov

Ivan Climov

  • 969
  • 685
  • 21.3k

How to filter “table_2” depending on the selected entry in “

Feb 23 2019 1:03 AM
I'm using:
  - Entity Framework v6.2.0;
  - SQL Server 2012;
  - C#.
 
I have these tables:
 - tbl_01_Groups;
- tbl_02_Students;
- tbl_03_GroupsStud;
The tbl_01_Groups table is displayed in dataGridView1. One student can be in several groups.
To solve this problem, I suspect that it is necessary to build the table of relations tbl_03_GroupsStud.
Question: how to make dataGridView2 display table_2 records that belong to the group selected in dataGridView1?
Requirements for table_2:
 1.  table_2 should contain the following fields:
  • [tbl_02_Students].[NameStud];
  • [tbl_02_Students].[Property_1];
  • [tbl_01_Groups].[nameGroup];
  • ... other columns as needed
2. if changes are made in the table_2 record, then these changes are displayed in the original source;
3. if an entry is added to the table_2, then this entry is displayed in the source;
Table_2 is planned to be used in another query.
 
How to make the table_2 meet the above requirements?
Something like I managed to do with the help of queries in MSAccess:
Request req_GroupsStud_Stud:
  1. SELECT tbl_03_GroupsStud.*, tbl_02_Students.NameStud  
  2. FROM tbl_03_GroupsStud  
  3. INNER JOIN tbl_02_Students  
  4. ON tbl_03_GroupsStud.id_stud = tbl_02_Students.id_stud;   
Request req_GroupsStud_CurGroup
  1. SELECT req_GroupsStud_Stud.*, req_GroupsStud_Stud.id_group  
  2. FROM req_GroupsStud_Stud  
  3. WHERE (((req_GroupsStud_Stud.id_group)=[Forms]![frm_00_00_MainForm]![id_group_Frm]));  
From the form, using the expression [Forms]![Frm_00_00_MainForm]![Id_group_Frm], the parameter [id_group_Frm] is passed to the request.
I don’t understand how to do the same thing using the entity-framework and MS SQL Serever.
 
 
 
My code
  1. ContextDB cntxtDB;  
  2.   
  3. public Frm1UC()  
  4. {  
  5.     InitializeComponent();  
  6.   
  7.     cntxtDB = new ContextDB();  
  8. }  
  9.   
  10. private void Frm1UC_Load(object sender, EventArgs e)  
  11. {  
  12.     Fill_dataGridView1();  
  13. }  
  14.   
  15. public void Fill_dataGridView1()  
  16. {  
  17.     try  
  18.     {  
  19.         cntxtDB.tbl_01_Groups.Load();  
  20.         bs_Grid1.DataSource = cntxtDB.tbl_01_Groups.Local.ToBindingList();  
  21.         dataGridView1.DataSource = bs_Grid1;  
  22.     }  
  23.     catch (Exception ex)  
  24.     {  
  25.         string s = ex.Message;  
  26.         string t = ex.StackTrace;  
  27.         // throw;  
  28.         MessageBox.Show(s);  
  29.     }  
  30. }  
      
 
https://imageshost.ru/image/Mbssq
https://imageshost.ru/image/Mb936
https://imageshost.ru/image/Mb7DW
https://imageshost.ru/image/MbWj7