DataGridView - 父子数据库关系 - 更新子 DataGridView 数据

发布于 2024-11-09 01:08:09 字数 1668 浏览 0 评论 0原文

有人可以帮我做以下事情吗?我有两个 DataGridView 对象,每个对象显示一个 DataTable,其中两个数据表与以下代码相关:

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

两个 DataTable 实际上都是 List<> 。使用以下内容转换为 DataTables:`

    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 中记录的任何更改而更新。

我发现这些表需要通过绑定源互连;但是我不相信这里有真正的绑定源。

有人可以引导我走向正确的方向吗?谢谢。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

场罚期间 2024-11-16 01:08:09

有一篇 MSDN 文章显示了您想要执行的操作:

如何:创建主/详细信息使用两个 Windows 窗体 DataGridView 控件的窗体

以下是这对您来说可能的工作方式:

通过设计器或通过代码将两个 BindingSource 添加到您的项目:parentBindingSource 和 childBindingSource。然后尝试用这个代替您拥有的代码。

// 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";

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

How to: Create a Master/Detail Form Using Two Windows Forms DataGridView Controls

Here's how this might work for you:

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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文