显示在DataGridView的父所有子记录 [英] Display all child records of a parent in DataGridView

查看:117
本文介绍了显示在DataGridView的父所有子记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的.NET,我真的不明白它是如何工作的。我有这样的项目,我必须做的,我问来显示所有父记录,当我选择一个父记录也应显示其所有的孩子。到目前为止,我成功地显示所有的父记录,使用一个DataGridView。

 私人无效display_btn_Click(对象发件人,EventArgs的)
        {
            dg.DataSource = data_set.Tables [0];
        }
 

下面code ++工程,但它显示从孩子的所有记录。我知道,我应该从父比较莫名其妙的主键与孩子和显示从子外键只有那些谁都是平等的与父母的PK,但我不知道怎么写呢。

 私人无效dg_CellContentClick(对象发件人,DataGridViewCellEventArgs E)
        {

            dg2.DataSource = data_set.Tables [1];

        }
 

在$ C $下创建的关系

 的DataColumn parentColumn = data_set.Tables [航空公司]列[airline_id]。
                DataColumn的childColumn = data_set.Tables [平面]列[airline_id]。

                rel_pln_air =新的DataRelation(fk_pln_air,parentColumn,childColumn);
                data_set.Relations.Add(rel_pln_air);
 

在code绑定:

  parentBindingSource.DataSource = data_set;
            parentBindingSource.DataMember =航空公司;
            childBindingSource.DataSource = parentBindingSource;
            childBindingSource.DataMember =面;
 

解决方案

链接相同的列,因为我在评论
声明 使用GetChildRows走关系

的DataRow。 GetChildRows

I am new to .NET, and I don't really understand how it works. I have this project I have to do and I'm asked to display all parent records, and when I select a parent record there should be displayed all its children. So far I managed to display all the parent records, using a DataGridView.

private void display_btn_Click(object sender, EventArgs e)
        {
            dg.DataSource = data_set.Tables[0];
        }

The following code works but it displays all the records from the child. I know that I should compare somehow the primary key from the parent with the foreign key from the child and display from the child only those who are equal with the PK from the parent, but I don't know how to write it.

private void dg_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            dg2.DataSource = data_set.Tables[1];

        }

The code for creating the relation

 DataColumn parentColumn = data_set.Tables["Airline"].Columns["airline_id"];
                DataColumn childColumn = data_set.Tables["Plane"].Columns["airline_id"];

                rel_pln_air = new DataRelation("fk_pln_air", parentColumn, childColumn);
                data_set.Relations.Add(rel_pln_air);

The code for binding:

parentBindingSource.DataSource = data_set;
            parentBindingSource.DataMember = "Airline";
            childBindingSource.DataSource = parentBindingSource;
            childBindingSource.DataMember = "Plane";

解决方案

Link the same column as I stated in the comment
Use GetChildRows to walk the relationship

DataRow.GetChildRows

这篇关于显示在DataGridView的父所有子记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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