获取数据行的子项

时间:2014-04-08 05:34:17

标签: c# sql .net

我一直在尝试从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";   
}

数据内容Clickgridview的Click事件如下:

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

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

任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:1)

您需要将DataRelation的名称指定为子BindingSource的DataMember,而不是子DataTable的名称。