I'm trying to create a two-datagridview Master-Detail form to display the results of an SQL statement, but appear to have an added complication in that the two results I want to display are from the same table: I want just the IDs to appear in the top grid, and then detail from the rest of the table about the same ID to appear in the bottom grid. If I'm using the wrong method, then could someone please point me in the wrong direction?
My current problem is that the code gets to the DataRelation Rel = new DataRelation....
and displays the form (with both DataGrids blank) at that point, rather than executing the rest of the code.
I've pasted the full version below:
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"]);
Code stops executing on the above line
Data.Relations.Add(Rel);
masterBindingSource.DataSource = Data;
masterBindingSource.DataMember = "[TradeFiles].[dbo].[OMG-Rejects]";
detailsBindingSource.DataSource = masterBindingSource;
detailsBindingSource.DataMember = "RejectDetail";
}
Managed to solve this by creating a view on the SQL server that mimicked the first select statement I was trying to acheive. Then I just referenced the view in the masterDataAdapter, and retrieved the rest of the information from the underlying table in the detailDataAdapter.