在一个datagridview中显示父数据表并在另一个中显示子数据表元素?

编程入门 行业动态 更新时间:2024-10-25 08:21:18
本文介绍了在一个datagridview中显示父数据表并在另一个中显示子数据表元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好,我创建了一个数据集和两个数据表.URLData 表通过关键字列作为 KeywordData 表的父级.

Hi I have created a dataset and two datatables. The URLData table is parented to the KeywordData table via the keyword column.

'Create the primary object to hold both data tables Dim AllKeyWordInfo As DataSet = New DataSet("AllKeywordInfo") 'Create a Datatable For keyword Data. Data Tables are stores In the Dataset Dim KeywordData As DataTable = AllKeyWordInfo.Tables.Add("Keywords") 'Create a Datatable For URL Data. Dim URLData As DataTable = AllKeyWordInfo.Tables.Add("URLMetrics") 'Add Columns to our Keyword datatable KeywordData.Columns.Add("Keyword") KeywordData.Columns.Add("SearchCount") 'Add Columns to URLDATA URLData.Columns.Add("Keyword") URLData.Columns.Add("URL") URLData.Columns.Add("DA") URLData.Columns.Add("PA") 'Creat a parent child relationship AllKeyWordInfo.Relations.Add("ALLDATA", AllKeyWordInfo.Tables("Keywords").Columns("Keyword"), AllKeyWordInfo.Tables("URLMetrics").Columns("Keyword")) 'parent KeywordData.Rows.Add("TESTEST", "1123829") 'children URLData.Rows.Add("TESTEST", "288789") URLData.Rows.Add("TESTEST", "asdsdsdd") DataGridView1.DataSource = KeywordData DataGridView2.DataSource = URLData

我想做的是显示 datagridview1 中 KeywordData 表中的所有内容.当用户单击任何行(在数据网格上启用全行选择)时,我希望 datagridview2 显示该关键字的所有子行.每当用户切换 datagridview1 中的行时,它都会切换到 datagridview2 中的正确子行.这可能吗?

What I would like to do is show everything from the KeywordData table in datagridview1. When a user click on any row (full row select enabled on the datagrid) I would like datagridview2 to show all the child rows for that keyword. Any time the user switches rows in datagridview1 it switches to the proper child rows in datagridview2. Is this possible?

推荐答案

诀窍在于绑定.点击这里获取完整的解决方案,包括创建DataSet:

The trick is in the binding. Click here for the full solution, which includes creating the DataSet:

这是重要的部分,考虑到您已经有一个 DataSet 包含两个 DataTables 和一个 DataRelation:

Here's the important part, considering that you already have a DataSet containing two DataTables and a DataRelation:

'Bind the parent source to the parent table. Me.BindingSource1.DataSource = data Me.BindingSource1.DataMember = "Parent" 'This is the name of the parent DataTable. 'Bind the child source to the relationship. Me.BindingSource2.DataSource = Me.BindingSource1 Me.BindingSource2.DataMember = "ParentChild" 'This is the name of the DataRelation. 'Bind the parent control to the parent source. Me.DataGridView1.DataSource = Me.BindingSource1 'Bind the child control to the child source. Me.DataGridView2.DataSource = Me.BindingSource2

在这种情况下,data 是 DataSet.请注意,父 BindingSource 通过 DataSet 绑定到父 DataTable 而子 BindingSource 绑定到DataRelation 通过父 BindingSource,而不是绑定到子 DataTable.

In this case, data is the DataSet. Note that the parent BindingSource is bound to the parent DataTable via the DataSet while the child BindingSource is bound to the DataRelation via the parent BindingSource, rather than being bound to the child DataTable.

还请注意,我的原始示例绑定到 ComboBox 控件,但正如我在该线程中所说,无论控件的类型如何,原理都是相同的.我已经编辑了上面的代码,改为使用 DataGridView 控件.

Also note that my original example binds to ComboBox controls but, as I say in that thread, the principle is the same regardless of the type of the controls. I've edited the code above to use DataGridView controls instead.

更多推荐

在一个datagridview中显示父数据表并在另一个中显示子数据表元素?

本文发布于:2023-10-16 10:35:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497321.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据表   并在   元素   datagridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!