Имеется БД Access из двух связанных таблиц (связь один ко многим; одной строке таблицы1 соответствует несколько строк таблицы2). На форме два датагдида для отображения обоих таблиц БД.
Пытаюсь решить следующую задачу:
Необходимо чтобы при выборе строки датагрида таблицы1 в датагриде таблицы2 отображались только связанные с выделенной строкой данные, т.е. простая выборка.
Пытался сделать как показано здесь (т.е. в источник данных таблицы2 поставить связь таблиц), но второй датагрид упрямо показывает всю таблицу2.
Код формы:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace v1._0
{
public partial class Form6DB : Form
{
public Form6DB()
{
InitializeComponent();
}
private void Form6DB_Load(object sender, EventArgs e)
{
this.model_ParamTableAdapter.Fill(this.dataBaseMDBDataSet._Model_Param);
nameFirmModelParamBindingSource.DataSource = this.dataBaseMDBDataSet._Model_Param;
this.name_FirmTableAdapter.Fill(this.dataBaseMDBDataSet.Name_Firm);
nameFirmBindingSource.DataSource = this.dataBaseMDBDataSet.Name_Firm;
}
private void buttonSaveMainTable_Click(object sender, EventArgs e)
{
try
{
nameFirmBindingSource.EndEdit();
name_FirmTableAdapter.Update(dataBaseMDBDataSet.Name_Firm);
nameFirmModelParamBindingSource.EndEdit();
model_ParamTableAdapter.Update(dataBaseMDBDataSet._Model_Param);
panel1.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
nameFirmBindingSource.ResetBindings(false);
}
}
private void buttonSaveSecondTable_Click(object sender, EventArgs e)
{
try
{
nameFirmModelParamBindingSource.EndEdit();
model_ParamTableAdapter.Update(dataBaseMDBDataSet._Model_Param);
nameFirmBindingSource.EndEdit();
name_FirmTableAdapter.Update(dataBaseMDBDataSet.Name_Firm);
panel2.Enabled = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
nameFirmModelParamBindingSource.ResetBindings(false);
}
}
private void buttonNewMainTable_Click(object sender, EventArgs e)
{
try
{
panel1.Enabled = true;
textBoxNameFirm.Focus();
dataBaseMDBDataSet.Name_Firm.AddName_FirmRow(this.dataBaseMDBDataSet.Name_Firm.NewName_FirmRow());
nameFirmBindingSource.MoveLast();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
nameFirmBindingSource.ResetBindings(false);
}
}
private void buttonNewSecondTable_Click(object sender, EventArgs e)
{
try
{
panel2.Enabled = true;
textBox1.Focus(); dataBaseMDBDataSet._Model_Param.Add_Model_ParamRow(this.dataBaseMDBDataSet._Model_Param.New_Model_ParamRow());
nameFirmModelParamBindingSource.MoveLast();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
nameFirmModelParamBindingSource.ResetBindings(false);
}
}
private void buttonEditMainTable_Click(object sender, EventArgs e)
{
panel1.Enabled = true;
textBoxNameFirm.Focus();
}
private void buttonEditSecondTable_Click(object sender, EventArgs e)
{
panel2.Enabled = true;
textBox1.Focus();
}
private void buttonDeleteMainTable_Click(object sender, EventArgs e)
{
try
{
if (MessageBox.Show("Удалить запись?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
string str;
str = Convert.ToString(dataGridView1.CurrentRow.Cells[0].Value);//захват значения индекса строки из первой таблицы
for (int i = 0; i < dataGridView1.RowCount; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value.ToString() == str)
{
dataBaseMDBDataSet.Name_Firm.Rows[i].Delete();
model_ParamTableAdapter.Update(dataBaseMDBDataSet._Model_Param);
name_FirmTableAdapter.Update(dataBaseMDBDataSet.Name_Firm);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonDeleteSecondTable_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить запись?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
nameFirmModelParamBindingSource.RemoveCurrent();
model_ParamTableAdapter.Update(dataBaseMDBDataSet._Model_Param);
}
}
private void buttonCanselMainTable_Click(object sender, EventArgs e)
{
panel1.Enabled = false;
nameFirmBindingSource.CancelEdit();
nameFirmBindingSource.ResetBindings(false);
}
private void buttonCanselSecondTable_Click(object sender, EventArgs e)
{
panel2.Enabled = false;
nameFirmModelParamBindingSource.CancelEdit();
nameFirmModelParamBindingSource.ResetBindings(false);
}
}
}
BindingSource
должен указывать на источник данных, второйBindingSource
должен указывать на первыйBindingSource
.nameFirmModelParamBindingSource.DataSource = this.dataBaseMDBDataSet._Model_Param;// проблема в этой строчке?