locked
How to filter DataGrid by selected value of ComboBox RRS feed

  • Question

  • Hi All.

    In my form I would like to populate DataDrid by selecting value of ComboBox. The ComboBox populated with that method

    public static List<Department> GetDepartments()
    {
        List<Department> deptdg = new List<Department>();
        using (Model1 context = new Model1())
        {
            var departments = context.Departments.AsNoTracking().ToList();
            foreach (var dt in departments)
            {
                Department info = new Department();
                info.DepartmentId = dt.DepartmentId;
                info.Department = dt.Department;
                info.Description = dt.Description;
    
                deptdg.Add(info);
            }
        }
        return deptdg;
    }
    


    And DataGrid is populated by this method

    public static IEnumerable<DepartmentEmployee> GetDepartmentEmployee()
    {
        using (var context=new Model1())
        {
            var deptemplg =
                from departmentemployee in context.DepartmentEmployees
                join employee in context.Employees
                on departmentemployee.EmpId equals employee.EmpID
                select new DepartmentEmployee
                {
                    EmpName = employee.EmployeeName,
                    IsActive = departmentemployee.IsActive,
                    CreatedDate = departmentemployee.CreatedDate,
                    CreatedBy = departmentemployee.CreatedBy,
                    UpdatedDate = departmentemployee.UpdatedDate,
                    UpdatedBy = departmentemployee.UpdatedBy
                };
            return deptempdg.ToArray();
        }
    }
    

    The entity table DepartmentEmployee has field DepartmentId like foreign key of entity of Department table. How to modify those methods to filter DataGrid by selected ComboBox value?

    Thanks.

    Tuesday, August 25, 2020 7:41 PM

All replies

  • The `GetDepartmentEmployee` method needs to accept the ID of the department as a parameter. Then inside the LINQ query filter out those that aren't in the department. Something like this perhaps.

    public static IEnumerable<DepartmentEmployee> GetDepartmentEmployee ( int departmentId )
    {
       using (...)
       {
           var deptemplg = from ...
                           join ...
                           where departmentemployee.DeptId //Guessing column name here
                                == departmentId
                           select ...;           
     
       };
    }

    As for how you pass that ID to the method you'll have to change your binding call to pass in the ID given the selected combo item's ID, if any. Something like this.

    //Some method called when the combo changes
    void WhenComboChanges ()
    {
       //Get selected department from UI
       var dept = ...
       if (dept != null)
          var employees = GetDepartmentEmployee(dept.DepartmentId);
    }
    But all this is highly dependent upon how you're doing that data binding. This forum is only focused on C#. If you need help with getting the actual data binding in the UI to work then we can move your question to the Winforms forum where they can better help you with the data binding side of the fence.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, August 25, 2020 9:44 PM
  • Hi CoolDadTx. Thank for reply.

    If I will modify GetDepartment method to return only DepartmentId parameter. 

    public static List<int> GetDepartments()
    {
        using (var context = new Model())
        {
            List<int> deptid = context.Departments.Select(d => d.DepartmentId).ToList();
    
            return deptid;
        }
    }


    I that case I cannot use that method to populate ComboBox. Because I will have only list of DepartmentId and don't have list of DepartmentName. How to modify that method to pass DepartmentId and have source to populate ComboBox by list of DepartmentName?

    Thanks.

    Tuesday, August 25, 2020 10:23 PM
  • The `GetDepartments` method remains unchanged. Leave it as you originally had it. I assume you're binding that list to your combo box. Therefore you have access to all the properties on that object.  Post your code that you are using to bind the controls to the methods you gave.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, August 26, 2020 1:17 AM
  • Hi zleug,

    Thank you for posting here.

    Please try the following code .

            public MainWindow()
            {
                InitializeComponent();
                List<Department> departments = GetDepartments();
                comboBox1.ItemsSource = departments;
                comboBox1.DisplayMemberPath = "DepartmentName";
            }
            private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                var selectedDep = (Department)comboBox1.SelectedItem;
                List<DepartmentEmployee> re = GetDepartments(selectedDep.DepartmentID);
                dataGrid1.ItemsSource = re;
    
            }
            private List<Department> GetDepartments()
            {
                using (Model1 content = new Model1())
                {
                    return content.Departments.ToList();
                }
            }
            private List<DepartmentEmployee> GetDepartments(int departmentID)
            {
                using (Model1 content = new Model1())
                {
                  return content.People.ToList().Where(peo => peo.DepartmentID == departmentID).ToList();
                }
            }

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 26, 2020 5:55 AM
  • Hi,

    Has your issue been resolved?

    If so, please click on the "Mark as answer" option of the reply that solved your question, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 2, 2020 9:10 AM