绑定datagrid时出现问题 [英] Problem in binding datagrid
问题描述
嗨......
我正在努力向datagrid显示值。我有两个类Employee,Department。
Department-DeptId,DeptName
Employee- EmpId,EmpName,EmpSalary,DepartmentValues的集合。使用List我将这些属性值添加到类中。
我必须按列表进行分组,并使用类似于lambda表达式的dept id,结果应该转换为Todictionary。最后输出必须绑定到datagrid。我试过这样.Datagrid没有绑定。
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;
命名空间 displays_tables
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
public class 员工
{
private Form1.Department dept1;
public 员工(Form1.Department dept1)
{
/ / TODO:完成成员初始化
此 .dept1 = dept1;
}
public int empId {获得跨度>; set ; }
public string empName { get 跨度>; set ; }
public int empSalary { get 跨度>; set ; }
public 部门部门{ get ; set ; }
// 公共覆盖字符串ToString()
// {
// 返回this.empName;
// }
}
public class 部门
{
< span class =code-keyword> public int deptId { get ; set ; }
public string deptName { get 跨度>; set ; }
}
private void Form1_Load( object sender,EventArgs e)
{
// 列表< List< Employee>> master = new List< List< Employee>>();
List< Department> list = new 列表< Department>();
Department dept = new Department();
dept.deptId = 1 ;
dept.deptName = IT;
list.Add(dept);
列表<员工> list1 = new 列表< Employee>();
员工emp = 新员工(部门);
emp.empId = 101 ;
emp.empName = anusha;
emp.empSalary = 9000 ;
emp.Department = dept;
list1.Add(emp);
emp = new 员工(部门);
emp.empId = 102 ;
emp.empName = Sindhu;
emp.empSalary = 9000 ;
emp.Department = dept;
list1.Add(emp);
dept = new 部门();
dept.deptId = 2 ;
dept.deptName = HR;
list.Add(dept);
emp = new 员工(部门);
emp.empId = 103 ;
emp.empName = usha;
emp.empSalary = 2000 ;
emp.Department = dept;
list1.Add(emp);
emp = new 员工(部门);
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)。其中(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})。其中(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;
}
}
}
在Datagrid中,列应为DeptId, EmpId,EmpName.And我也必须得到相同的Deptid
-Thanks in Advance
< blockquote>
Quote:第一件事DataGridView不支持Dictionary集合。 DataGridView类支持标准的Windows窗体数据绑定模型。这意味着数据源可以是实现以下接口之一的任何类型:
1. IList接口,包括一维数组。
2.IListSource接口,例如DataTable和DataSet类。
3.IBindingList接口,例如BindingList< T>。 class.
4. IBindingListView接口,例如BindingSource类。
引用:第二件事如果具有相同键的元素已经存在于Dictionary中,那么将通过'ArgumentException'。
var query4 = list1.Select(t => new {t.empId,t.Department.deptId})。ToDictionary(t => ; t.empId,t => t.deptId)。ToList();
dataGridView5.DataSource = query4;
如果你将deptId与字典中的empId交换,你的代码将不会给出异常。
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.
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
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 '.
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.
这篇关于绑定datagrid时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!