结合与主/班分成两个datagridview的 [英] Binding class with master/detail into two datagridview

查看:121
本文介绍了结合与主/班分成两个datagridview的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提出了在C#Windows窗体类重新present我的数据库。
它使用列表℃的主/细节;>​​

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

与员工个人资料的记录与培训(主)和TrainingDetails(详细)

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

现在,我怎么能显示此为2 datagridview的,每当我从第一个datagridview的一个培训,它会显示在2 datagridview的细节。

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.

它很容易改变第二DataGridView的数据源每当用户从第一的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照顾这个给你。

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; }
}

您可以创建它重新presents主/详细情况教师的名单:

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);

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

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

我的一切丝像这样:

    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.

有关管理插入,更新和删除您将需要实现某种变化跟踪自己(或使用ORM,如实体框架或NHibernate的)。这是值得它自己的问题,这些技术使阅读起来(并期待在博客帖子下面我喜欢的),回来时,你有一些特定的问题的一个话题。

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.

有关这个答案我借巨资从<一个href=\"http://www.softinsight.com/bnoyes/2005/11/09/BuildCustomDataBoundBusinessObjectsAndCollectionsTalkAtDevConnectionsThisMorning.aspx\">this优秀的后 - 我给出的例子是完整的,避免了大量的中,作者的例子的复杂性,但最终你可能会想,至少知道他讨论的一切。下载他的演示和看看。

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的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆