C# DataGridView-父到子数据库关系-更新子DataGridView数据

C# DataGridView-父到子数据库关系-更新子DataGridView数据,c#,datagridview,parent-child,relationship,C#,Datagridview,Parent Child,Relationship,有人能帮我做以下事情吗?我有两个DataGridView对象,每个对象显示一个DataTable,其中两个DataTable与以下代码相关: DataSet dSet = new DataSet(); DataTable ParentList = ListToDataTable(_listOfAllAlbumObjects); DataTable ChildList = ListToDataTable(_listOfAllTrackObjects); dSet.Tables.AddRange(n

有人能帮我做以下事情吗?我有两个DataGridView对象,每个对象显示一个DataTable,其中两个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";
    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;
          }
这两个数据表实际上都是列表转换为数据表,具有以下特性:`

    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;
          }
公共静态数据表ListToDataTable(IList数据)
{
var props=TypeDescriptor.GetProperties(typeof(T));
var table=新数据表();
对于(变量i=0;i
最初,似乎每个DataGridView都适当地显示了数据;但是,子DataGridView不会随父DataGridView中记录的任何更改而更新

    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;
          }
我发现这些表需要通过绑定源进行互连;然而,我不相信这里有真正的绑定源

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

有人能把我引向正确的方向吗?谢谢。

有一篇MSDN文章显示了您想要做的事情:

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

    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;
          }
以下是这可能对您起作用的方式:

    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;
          }
通过设计器或代码将两个BindingSource添加到项目中:parentBindingSource和childBindingSource。然后试着用这个代替你的代码

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

杰-谢谢你的回复。但是,正如您将看到的,这两个数据表都是转换列表(请参见上面添加的代码)。虽然我可以将“父”数据库的数据源设置为“dSet”(数据集),将DataMember设置为第一个表,但我不确定如何将子表的数据源链接到BindingSource,因为没有使用BindingSource。有什么建议吗?@Bill数据表来自哪里并不重要,只要它们在数据集中正确相关。不过,您需要使用BindingSources来管理DataGridView中的关系(遵循我链接的文章中的模式)。我的编辑应该会有帮助。谢谢你抽出时间。您建议的修改非常有效,并且注释使其易于理解。再次感谢!这是非常古老的,但我想知道是否有一种方法可以将此方法应用于EF4。父绑定似乎可以工作,但子绑定不会提取关联的数据。我想知道是否有一个调整可以使它工作。@Pluc-对不起,我不知道。还没有玩过EF。祝你好运