我正在尝试创建一个包含两个数据网格视图的主从表单来显示 SQL 语句的结果,但似乎有一个额外的复杂性,因为我要显示的两个结果来自同一个表:我只想要 ID出现在顶部网格中,然后从表格的其余部分详细了解相同的 ID 出现在底部网格中。如果我使用了错误的方法,那么有人可以指出我错误的方向吗?
我当前的问题是代码到达DataRelation Rel = new DataRelation....
并显示表单(DataGrids 均为空白),而不是执行其余代码。
我在下面粘贴了完整版:
private void Form1_Load(object sender, EventArgs e)
{
SpContain.Panel1.Controls.Add(masterDataGridView);
SpContain.Panel2.Controls.Add(detailsDataGridView);
masterDataGridView.DataSource = masterBindingSource;
detailsDataGridView.DataSource = detailsBindingSource;
GetData();
}
private void GetData()
{
string ConnStr = "Server=TradarUAT\\uattradar; Integrated Security=SSPI; Initial Catalog=TradeFiles;";
SqlConnection Conn = new SqlConnection(ConnStr);
DataSet Data = new DataSet();
Data.Locale = System.Globalization.CultureInfo.InvariantCulture;
SqlDataAdapter masterDataAdapter = new SqlDataAdapter("Select MasterReference from [TradeFiles].[dbo].[OMG-Rejects] GROUP BY MasterReference", Conn);
masterDataAdapter.Fill(Data, "[TradeFiles].[dbo].[OMG-Rejects]");
SqlDataAdapter detailDataAdapter = new SqlDataAdapter("Select ImportedDT,TypeIndicator,FileNumber,MasterReference,ClientAlocRef,VersionNumber,DateTimeStamp,ErrorID,ErrorKey,ErrorTxt,ErrorParamType,ErrorParamValue from [TradeFiles].[dbo].[OMG-Rejects]", Conn);
detailDataAdapter.Fill(Data, "[TradeFiles].[dbo].[OMG-Rejects]");
DataRelation Rel = new DataRelation("RejectDetail",
Data.Tables[0].Columns["MasterReference"], Data.Tables[1].Columns["MasterReference"]);
代码在上述行停止执行
Data.Relations.Add(Rel);
masterBindingSource.DataSource = Data;
masterBindingSource.DataMember = "[TradeFiles].[dbo].[OMG-Rejects]";
detailsBindingSource.DataSource = masterBindingSource;
detailsBindingSource.DataMember = "RejectDetail";
}