Click here to Skip to main content
15,887,262 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hi...
I am working on displaying values to datagrid. I have two classes Employee,Department.
Department-DeptId,DeptName
Employee- EmpId,EmpName,EmpSalary,Collection of DepartmentValues. With List i added these property values to class.
I have to group by the list with similar dept id with lambda expression and result should be converted to Todictionary. Finally output must be binded to datagrid. I tried like this.Datagrid is not getting binded.
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace displaying_tables
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public class Employee
        {
       
        private Form1.Department dept1;
 
        public Employee(Form1.Department dept1)
        {
            // TODO: Complete member initialization
            this.dept1 = dept1;
        }
        public int empId { get; set; }
        public string empName { get; set; }
        public int empSalary { get; set; }
        public Department Department { get; set; }
        //public override string ToString()
        //{
        //    return this.empName;
        //}
    }
        public class Department
        {
            public int deptId { get; set; }
            public string deptName { get; set; }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            //List <List<Employee>> master=new List<List<Employee>>();
            List<Department> list = new List<Department>();
            Department dept = new Department();
            dept.deptId = 1;
            dept.deptName = "IT";
            list.Add(dept);
            List<Employee> list1 = new List<Employee>();
            Employee emp = new Employee(dept);
            emp.empId = 101;
            emp.empName = "anusha";
            emp.empSalary = 9000;
            emp.Department = dept;
            list1.Add(emp);
            emp = new Employee(dept);
            emp.empId = 102;
            emp.empName = "Sindhu";
            emp.empSalary = 9000;
            emp.Department = dept;
            list1.Add(emp);
            dept = new Department();
            dept.deptId = 2;
            dept.deptName = "HR";
            list.Add(dept);
            emp = new Employee(dept);
            emp.empId = 103;
            emp.empName = "usha";
            emp.empSalary = 2000;
            emp.Department = dept;
            list1.Add(emp);
            emp = new Employee(dept);
            emp.empId = 104;
            emp.empName = "indhu";
            emp.empSalary = 3000;
            emp.Department = dept;
            list1.Add(emp);
            //var lamdaquery = master.SelectMany(r => r).Where(m=>m.empId==101);
            //var query = list1.Select(r => r).Where(m => m.empId >100);
            var query = list1.Select(x => new { x.empId, x.empName, x.empSalary, x.Department.deptId }).ToList();
            dataGridView1.DataSource = query;
            var query1 = list1.Select(x => new { x.empId, x.empName, x.empSalary, x.Department.deptId,x.Department.deptName }).ToList();
            dataGridView2.DataSource = query1;
            var query2 = list1.Select(x => new { x.empId, x.empName, x.empSalary, x.Department.deptId, x.Department.deptName }).Where(m => (m.empSalary > 1000 && m.empSalary < 5000)).ToList();
            dataGridView3.DataSource = query2;
            var query3 = list1.Select(x => new { x.empId, x.empName, x.empSalary, x.Department.deptId, x.Department.deptName }).OrderByDescending(m => m.empSalary);
            dataGridView4.DataSource = query3.ToList();
            var query4 = list1.Select(t=>new{t.Department.deptId,t.empId}).ToDictionary(t => t.deptId,t=>t.empId);
            dataGridView5.DataSource = query4;
           
        }
    }
}


In Datagrid the columns should be DeptId,EmpId,EmpName.And also I have to get count of the Same Deptid

-Thanks in Advance
Posted
Comments
Cenarjun 13-Jan-14 5:08am    
Where are u binding the data after assigning the data source? What error r u getting?
bowlturner 13-Jan-14 9:47am    
according to you code, you page has 5 dataGridViews. What you are saying and what your code is doing to not match up.

1 solution

Quote:
First thing The DataGridView don't support Dictionary collection. The DataGridView class supports the standard Windows Forms data-binding model. This means the data source can be of any type that implements one of the following interfaces:

1. The IList interface, including one-dimensional arrays.
2. The IListSource interface, such as the DataTable and DataSet classes.
3. The IBindingList interface, such as the BindingList<T> class.
4. The IBindingListView interface, such as the BindingSource class.
Quote:
Second thing If an element with the same key already exists in the Dictionary if will through 'ArgumentException '.

XML
 var query4 = list1.Select(t => new { t.empId, t.Department.deptId }).ToDictionary(t => t.empId, t => t.deptId).ToList();
dataGridView5.DataSource = query4;

your code will not give exception if you swap the deptId with empId in Dictionary.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900