DataGridView - Parent to Child Database Relation - Updating child DataGridView data

泪湿孤枕 提交于 2019-12-30 14:30:13

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/6063902/datagridview-parent-to-child-database-relation-updating-child-datagridview-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!