I have an application developed C#
using the Windows Forms
project that displays the Database People Table in a list in the DataGridView
field. However I need to display in the DataGridView
also the Endereco
table.
Note: relationship is 1 to 1 .
Iusethecodebelowtohavetheresultasaboveprint,
this.DataGridView1Pessoa.DataBindings.Add("DataSource",this, "ListPessoas");
this.DataGridView1Pessoa.AutoGenerateColumns = false;
"This generates two joins in the collection to bind to the same property. Parameter name: binding".
I have to display the result of this select below in Endereco
.
Endereco
and for this I created the method below:
public SqlDataReader ListarPessoas()
{
SqlCommand query = new SqlCommand();
query.CommandText = ("select * " +
"from Table_Pessoas " +
"left outer join Table_Endereco_Pessoa on Table_Endereco_Pessoa.Id_EnderecoPessoa = Table_Pessoas.Id_EnderecoPessoa " +
"left outer join Table_Endereco end_pessoa on end_pessoa.Id_Endereco = Table_Endereco_Pessoa.Id_Endereco " +
"left outer join Table_Orgao_Emissor_Rg on Table_Pessoas.Id_RgOrgEmiss = Table_Orgao_Emissor_Rg.Id_RgOrgEmiss");
return (query.ExecuteReader());
}
I know that to execute this method I need to pass the "query.Connection" property to connect to DB, but I have already done several searches and I did not find what I need to do to make this connection work in my application.
To display the Person table in dgv I use the connection to bd through the app.config file.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="WindowsFormsApplication1Fams.Properties.Settings.FAMSConnectionString"
connectionString="Data Source=DESKTOP-4KTTTDL\INSTANCEFAMS;Initial Catalog=BD_Fams;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="FamsContext" connectionString="Data Source=DESKTOP-4KTTTDL\INSTANCEF;Initial Catalog=BD_F;Persist Security Info=True;User ID=sa;Password=f;MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="WindowsFormsApplication1Fams.Properties.Settings.FAMSConnectionString"
connectionString="Data Source=DESKTOP-4KTTTDL\INSTANCEFAMS;Initial Catalog=BD_Fams;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="FamsContext" connectionString="Data Source=DESKTOP-4KTTTDL\INSTANCEFAMS;Initial Catalog=BD_Fams;Persist Security Info=True;User ID=sa;Password=f@m5;MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
I use the Table_Pessoa class inside the Data Folder. Where it contains the Get and Set. And I use a PersonModel class inside the Model Folder. As below:
public class PessoaModel : DataContext<Table_Pessoas>
{
public IEnumerable<Table_Pessoas> CarregarTodos()
{
return new ObservableCollection<Table_Pessoas>(this.Context.Pessoas);
}
}
And in the form class. I load the DB data. Through the Load event below.
private IEnumerable<Table_Pessoas> listPessoa;
private void FrmCadastrarPessoa_Load(object sender, EventArgs e)
{
this.ListPessoas = PessoaModel.CarregarTodos();
}
And in this same class in the method below to finally pass the data from the Person Table to dgv.
public FrmCadastrarPessoa()
{
InitializeComponent();
this.DataGridView1Pessoa.DataBindings.Add("DataSource", this, "ListPessoas");
this.DataGridView1Pessoa.AutoGenerateColumns = false;
}
The problem now is how to implement the dgv-DataGridView
property of my "select * from Table_Pessoas left outer join Table_Endereco_Pessoa on Table_Endereco_Pessoa.Id_EnderecoPessoa = Table_Pessoas.Id_EnderecoPessoa left outer join Table_Endereco end_pessoa on end_pessoa.Id_Endereco = Table_Endereco_Pessoa.Id_Endereco"
method and display its return on query.Connection
?