Witam wszystkich serdecznie. Męczę się i męczę, niby znalazłem przykłady w sieci ale niestety nie działają, może ktoś będzie znał rozwiązanie mojego problemu, który wygląda następująco. Mam dwie tabele, sl_kontr i sl_kontr_adres. W tabeli sl_kontr pola, które nas interesując to "id" PK IdentityStep = 1 i powiedzmy "nazwa". Tabela sl_kontr_adres to "id" PK IdentityStep = 1 i id_sl_kontr, który tworzy relację z tabelą sl_kontr. Dalej mam dwie DataGridView. Chciałbym uzyskac taki efekt, że jak kliknę na pozycję w tabeli odpowiadającej sl_kontr to druga tabelka DataGridView przefiltruje się dla danego "id" z sl_kontr. Czyli w drugiej DataGridView pojawią sie tylko te pozycję dla których sl_kontr_adres.id_sl_kontr = sl_kontr.id. Wklejam kod, który niby powinien działac, źródło MSDN, ale niestety nie działa. Efekt jest taki, że w tabelce powiązanej z sl_kontr mam wszystkie rekordy i w drugiej też wszystkie sl_kontr_adres, czyli nie filtruje.
CreateDataSet("select * from sl_kontr", "sl_kontr");
adapterPoz = gcnew OdbcDataAdapter("select * from sl_kontr_adres", MyUtils::Globals::m_connection);
adapterPoz->Fill(GetDataSet(), "sl_kontr_adres");
adapterPoz->FillSchema(GetDataSet(), SchemaType::Mapped, "sl_kontr_adres");
DataRelation ^relation = gcnew DataRelation("Kontr-Adres",
GetDataSet()->Tables["sl_kontr"]->Columns["id"],
GetDataSet()->Tables["sl_kontr_adres"]->Columns["id_sl_kontr"]);
GetDataSet()->Relations->Add(relation);
mainBindingSource = gcnew BindingSource();
pozBindingSource = gcnew BindingSource();
mainBindingSource->DataSource = GetDataSet();
mainBindingSource->DataMember = GetDataSet()->Tables[0]->TableName;
pozBindingSource->DataSource = GetDataSet();
pozBindingSource->DataMember = GetDataSet()->Tables[1]->TableName;
dataGridViewMain->DataSource = nullptr;
dataGridViewMain->DataSource = mainBindingSource;
dataGridViewAdres->DataSource = pozBindingSource;
bool ^ BasicForm::CreateDataSet(String ^sql, String ^sTableName)
{
try
{
m_sTableName = sTableName;
dataAdapter = gcnew OdbcDataAdapter(sql, Globals::m_connection);
OdbcCommandBuilder ^builder = gcnew OdbcCommandBuilder(dataAdapter);
dataAdapter->InsertCommand = builder->GetInsertCommand();
dataAdapter->DeleteCommand = builder->GetDeleteCommand();
dataAdapter->UpdateCommand = builder->GetUpdateCommand();
dataSet = gcnew DataSet();
dataAdapter->FillSchema(dataSet, SchemaType::Mapped, m_sTableName);
dataAdapter->Fill(dataSet, m_sTableName);
GetDataGridView()->DataSource = dataSet->Tables[0];
}
catch(OdbcException ^e)
{
MessageBox::Show(e->Message, "Błąd połączenia");
return false;
}
return true;
}
Funkcje GetDataSet() zwraca dataSet, który jest tworzony w CreateDataSet