获取数据行的子项

本文关键字:数据 获取 | 更新日期: 2023-09-27 17:57:11

我一直在尝试从 dataGrid 显示某个选定父级的所有子项。我使用本教程作为指导 http://msdn.microsoft.com/en-us/library/vstudio/y8c0cxey%28v=vs.100%29.aspx 但没有运气。这是我的代码:

private void getData()
{
      SqlDataAdapter parentDataAdapter = new SqlDataAdapter("select * from Airline", connection);
      parentDataAdapter.Fill(ds, "Airline");
      SqlDataAdapter childDataAdapter = new SqlDataAdapter("select * from Plane", connection);
      childDataAdapter.Fill(ds, "Plane");
      DataColumn parentColumn = ds.Tables["Airline"].Columns["airline_id"];
      DataColumn childColumn = ds.Tables["Plane"].Columns["airline_id"];
      relation = new DataRelation("pln_air", parentColumn, childColumn);
      ds.Relations.Add(relation);
      parentBindingSource.DataSource = ds;
      parentBindingSource.DataMember = "Airline";
      childBindingSource.DataSource = parentBindingSource;
      childBindingSource.DataMember = "Plane";   
}

数据网格视图的单元格内容单击事件如下所示:

private void dg_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    parentDataGridView.DataSource = parentBindingSource;
    childDataGridView.DataSource = childBindingSource;
    getData();            
}

我的问题是,当我运行它并单击一个单元格时,出现错误:DataMember property 'Plane' cannot be found on the DataSource.

谁能帮我解决这个问题?

获取数据行的子项

您需要将 DataRelation 的名称指定为子 BindingSource 的数据成员,而不是子 DataTable 的名称。