DataGridView的 - 父到子数据库的关系 - 更新孩子的DataGridView数据 [英] DataGridView - Parent to Child Database Relation - Updating child DataGridView data

查看:186
本文介绍了DataGridView的 - 父到子数据库的关系 - 更新孩子的DataGridView数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

会有人好心帮助我下?我有两个DataGridView的对象,每个显示一个数据表,其中两个数据表都用下面的代码相关的:

Would someone kindly assist me with the following? I have two DataGridView objects that each display a DataTable, where the two datatables are related with the following code:

DataSet dSet = new DataSet();
DataTable ParentList = ListToDataTable(_listOfAllAlbumObjects);
DataTable ChildList = ListToDataTable(_listOfAllTrackObjects);
dSet.Tables.AddRange(new DataTable[]{ParentList, ChildList});
DataColumn parentRelationColumn = ParentList.Columns["AlbumId"];
DataColumn childRelationColumn = ChildList.Columns["AlbumId"];
dSet.Relations.Add("ParentToChild", parentRelationColumn, childRelationColumn);
ParentDataGridView.DataSource = dSet;
ParentDataGridView.DataMember = "ParentList";
ChildDataGridView.DataSource = ???;
ChildDataGridView.DataMember = "ParentToChild";



这两个数据表实际上是表<>转换为数据表与以下内容:`

Both DataTables are actually List<> converted to DataTables with the following:`

    public static DataTable ListToDataTable<T>( IList<T> data)
    {
              var props = TypeDescriptor.GetProperties(typeof(T));
              var table = new DataTable();
              for (var i = 0; i < props.Count; i++)
              {
                  PropertyDescriptor prop = props[i];
                  table.Columns.Add(prop.Name, prop.PropertyType);
              }
              var values = new object[props.Count];
              foreach (T item in data)
              {
                  for (int i = 0; i < values.Length; i++) 
                  { values[i] = props[i].GetValue(item); }
                  table.Rows.Add(values);
              }
              return table;
          }



起初似乎每个DataGridView中适当地显示数据;然而孩子的DataGridView不与在父DataGridView中记录的任何改变进行更新。

Initially it appears that the each DataGridView displays the data appropriately; however the child DataGridView does not update with any change of record in the parent DataGridView.

我看到表需要通过绑定源互连;然而,我不相信有真正的约束力源在这里。

I see that the tables need to be interconnected through the binding-source; however I don't believe there is a true binding-source here.

可能有人请我引导方向是正确的?谢谢

Could someone please steer me in the right direction? Thanks.

推荐答案

这里有一个MSDN文章显示你想要做什么:

There's an MSDN article showing what you want to do:

如何创建一个主/从窗体使用两个Windows窗体DataGridView控件

下面是如何这可能会为你工作:

Here's how this might work for you:

无论是通过设计师或通过代码添加两个BindingSources到您的项目:parentBindingSource和childBindingSource。然后,在地方,你的代码试试这个。

Either through the designer or through code add two BindingSources to your project: parentBindingSource and childBindingSource. Then try this in place of the code you have.

// Associate your BSs with your DGVs.
ParentDataGridView.DataSource = parentBindingSource;
ChildDataGridView.DataSource = childBindingSource;

// (Most of) your code here:
DataSet dSet = new DataSet();
DataTable ParentList = ListToDataTable(_listOfAllAlbumObjects);
DataTable ChildList = ListToDataTable(_listOfAllTrackObjects);
dSet.Tables.AddRange(new DataTable[]{ParentList, ChildList});
DataColumn parentRelationColumn = ParentList.Columns["AlbumId"];
DataColumn childRelationColumn = ChildList.Columns["AlbumId"];
dSet.Relations.Add("ParentToChild", parentRelationColumn, childRelationColumn);

// Let's name this DT to make clear what we're referencing later on.
ParentList.TableName = "ParentListDT";

// Rather than set the data properties on your DGVs, set them in your BindingSources.
parentBindingSource.DataSource = dSet;
parentBindingSource.DataMember = "ParentListDT";

childBindingSource.DataSource = parentBindingSource;
childBindingSource.DataMember = "ParentToChild";

这篇关于DataGridView的 - 父到子数据库的关系 - 更新孩子的DataGridView数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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