Good day. The question is this: There are 3 tables - Data, History, TypeHistory. Data and TypeHistory are two independent tables, and History depends on these two tables by a foreign key. Ie, History has two foreign keys. I create a DataSet that contains all these tables, and I create 3 DataGridView based on these tables. I need that when I move through the rows in a DataGridView that is tied to a TypeHistory, or move through the rows in a DataGridView that is tied to Data, automatic sampling takes place in the DataGridView, which is tied to History, based on foreign keys. I know that if there were two tables, for example, only Data and History, then such an automatic selection could be done like this: In the property (properties) of the DataGridView control, which is tied to History, transfer the dataBindingSource (from the Data table) to the DataSourse field, and in the DataMember field, pass "FK_History_ToData", that is, the foreign key that was created when creating the tables. Everything! And no code. After that, when switching between rows in a DataGridView tied to Data, the view automatically changes to the corresponding one in the DataGridView tied to History. How to deal with 3 tables that I described above?

Does anyone know?

  • It is necessary to change the binding of the History grid depending on which grid is now in focus. - Alexander Petrov
  • From this there will be no sense, since there the linkage from two Data Sources should take place, and not either by this or that. Simultaneously over two - Bretbas
  • Give an example of code that fills in these datatables (not from the database, but manually). And draw examples of the data, what data should be displayed. - Alexander Petrov
  • In short, I recorded a video, just like what I want to see, ie the History table depends on two foreign keys, and I need to fill with the DataGridView information only the one that depends on the Data table and TypeHistory on the foreign key. Here's a video link - Bretbas
  • Who will help? - Bretbas

1 answer 1

Nabil code create and populate the tables itself. If it does not coincide with yours - do not blame me.

Here is a fully working example. Data binding is used. And you only need a couple of lines of code in the event to change the filter. As a result, the contents of the third grid depends on the selected lines in the first two.

using System.Data; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { //InitializeComponent(); Size = new Size(500, 600); var dtData = new DataTable("Data"); dtData.Columns.Add("id", typeof(int)); dtData.Columns.Add("value", typeof(string)); dtData.Rows.Add(1, "data1"); dtData.Rows.Add(2, "data2"); var dtTypeHistory = new DataTable("TypeHistory"); dtTypeHistory.Columns.Add("id", typeof(int)); dtTypeHistory.Columns.Add("value", typeof(string)); dtTypeHistory.Rows.Add(11, "type1"); dtTypeHistory.Rows.Add(12, "type2"); var dtHistory = new DataTable("History"); dtHistory.Columns.Add("id", typeof(int)); dtHistory.Columns.Add("FK_Data", typeof(int)); dtHistory.Columns.Add("FK_TypeHistory", typeof(int)); dtHistory.Rows.Add(101, 1, 11); dtHistory.Rows.Add(102, 1, 12); dtHistory.Rows.Add(103, 2, 11); dtHistory.Rows.Add(104, 2, 12); var ds = new DataSet(); ds.Tables.Add(dtData); ds.Tables.Add(dtTypeHistory); ds.Tables.Add(dtHistory); ds.Relations.Add("FK_History_ToData", dtData.Columns["id"], dtHistory.Columns["FK_Data"]); ds.Relations.Add("FK_History_ToTypeHistory", dtTypeHistory.Columns["id"], dtHistory.Columns["FK_TypeHistory"]); var flp = new FlowLayoutPanel { Parent = this, Dock = DockStyle.Fill }; var dgvData = new DataGridView { Parent = flp, Width = 400 }; var dgvTypeHistory = new DataGridView { Parent = flp, Width = 400 }; var dgvHistory = new DataGridView { Parent = flp, Width = 400 }; var bsData = new BindingSource(); bsData.DataSource = ds; bsData.DataMember = "Data"; var bsTypeHistory = new BindingSource(); bsTypeHistory.DataSource = ds; bsTypeHistory.DataMember = "TypeHistory"; var bsHistory = new BindingSource(); bsHistory.DataSource = bsData; bsHistory.DataMember = "FK_History_ToData"; dgvTypeHistory.SelectionChanged += (s, e) => { string id = dgvTypeHistory.CurrentRow.Cells["id"].Value.ToString(); bsHistory.Filter = "FK_TypeHistory = " + id; }; dgvData.DataSource = bsData; dgvTypeHistory.DataSource = bsTypeHistory; dgvHistory.DataSource = bsHistory; } } } 

In your video, some kind of combo box appears. I did not delve into the nuances. But it’s not hard to add a BindingSource filter change on a combo box change event.