本文介绍了将类与主/细节绑定到两个datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#Windows窗体中创建了一个类来表示我的数据库。
它有一个主/详细使用列表<>



具有培训(主)和培训详细信息(详细信息)的员工配置文件的记录



现在,如何从第一个datagridview中选择培训,然后在第二个datagridview中显示详细信息,我如何将其显示为2 datagridview。



当用户从第一个datagridview中选择一个新项目时,它很容易更改第二个datagridview的数据源。但我想知道如何做专业。



同时保存是一个痛苦,我想通过数据行迭代并保存它,但这意味着我必须知道什么是更新,插入和删除的数据。



请帮助我。

解决方案

BindingSources为您处理此事。



例如说我有两个班,老师和学生:

  public class Teacher 
{
私人名单<学生> _students = new List< Student>();
public string Name {get;组; }
public string Class {get;组; }
public List< Student>学生{get {return _students; }}
}

public class Student
{
public string Name {get;组; }
public int Age {get;组; }
}

然后,您可以创建代表主/细节情况的教师列表:

 列表<教师> teacher = new List< Teacher>(); 
老师t = new Teacher();
t.Name =史密斯先生;
t.Class =A1;
teachers.Add(t);

学生s = new Student();
s.Name =吉米琼斯;
s.Age = 6;
t.Students.Add(s);

s = new Student();
s.Name =Jane Doe;
s.Age = 5;
t.Students.Add(s);

t = new Teacher();
t.Name =艾伦女士;
t.Class =B3;
teachers.Add(t);

s = new Student();
s.Name =Sally学生;
s.Age = 7;
t.Students.Add(s);

在我的表单上我有两个 DataGridViews teachersDataGridView studentsDataGridView 和两个绑定源 teachersBindingSource studentsBindingSource



我把所有东西都这样连接起来:



<$ p老师;教师;

studentsBindingSource.DataSource = teachersBindingSource;
studentsBindingSource.DataMember =学生;

teachersDataGridView.DataSource = teachersBindingSource;
studentsDataGridView.DataSource = studentsBindingSource;

如果在选择从教师网格中选择一个项目的表单上运行时,魔术变为学生网格






为了管理插入,更新和删除,您需要自己实现某种更改跟踪(或使用ORM如Entity Framework或nHibernate)。这是一个值得拥有的问题,所以请阅读这些技术(看看下面我喜欢的博客文章),当你有一些具体的问题时回来。






对于这个答案,我大量借用了优秀的帖子 - 我给出的例子是完整的,避免了作者的例子中的很多复杂性,但是最终你可能想要至少知道他讨论的一切。下载他的演示,看看。


I had made a class in C# Windows form to represent my database.It has a master/detail using List<>

A record with Employee profile with Trainings(master) and TrainingDetails(detail)

Now, how can I display this into 2 datagridview that whenever I select a "Training" from the first datagridview it will display the details on the 2nd datagridview.

Its easy to change the datasource of the 2nd datagridview whenever the user select a new item from the first datagridview. But im wondering how it is done professionally.

Also saving is a pain, Im thingking to iterate through the datarow and save it but It mean I have to know what are the data has been update, inserted and deleted.

Please help me. Im a newbie.

解决方案

BindingSources take care of this for you.

For example say I have two classes, Teachers and Students:

public class Teacher
{
    private List<Student> _students = new List<Student>();
    public string Name { get; set; }
    public string Class { get; set; }
    public List<Student> Students { get { return _students; } }
}

public class Student
{
    public string Name { get; set; }
    public int Age { get; set; }
}

You can then create a list of Teachers which represents the Master/Detail situation:

List<Teacher> teachers = new List<Teacher>();
Teacher t = new Teacher();
t.Name = "Mr. Smith";
t.Class = "A1";
teachers.Add(t);

Student s = new Student();
s.Name = "Jimmy Jones";
s.Age = 6;            
t.Students.Add(s);

s = new Student();
s.Name = "Jane Doe";
s.Age = 5;
t.Students.Add(s);

t = new Teacher();
t.Name = "Ms. Allen";
t.Class = "B3";
teachers.Add(t);

s = new Student();
s.Name = "Sally Student";
s.Age = 7;
t.Students.Add(s);

On my form I have two DataGridViews, teachersDataGridView and studentsDataGridView and two binding sources teachersBindingSource and studentsBindingSource.

I wire everything up like so:

    teachersBindingSource.DataSource = teachers;

    studentsBindingSource.DataSource = teachersBindingSource;
    studentsBindingSource.DataMember = "Students";

    teachersDataGridView.DataSource = teachersBindingSource;
    studentsDataGridView.DataSource = studentsBindingSource;

And as if by magic when running up on the form selecting an item from the teachers grid changes students grid.


For managing inserts, updates and deletes you will need to implement some sort of change tracking yourself (or use an ORM such as Entity Framework or nHibernate). It is a topic that deserves its own question so read up on those technologies (and look at the blog post I like below) and come back when you have some specific problems.


For this answer I borrowed heavily from this excellent post - the example I've given is complete and avoids a lot of the complexity in that authors example's, but eventually you will probably want to at least know about everything he discusses. Download his demos and have a look.

这篇关于将类与主/细节绑定到两个datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 16:05