3
I have an application developed C#
using the project Windows Forms
that displays the People Database Table in a list in the field DataGridView
. However I need to show in DataGridView
also the table Endereco
.
Observing: the relationship is 1 to 1.
I use the code below to have the result as shown above,
this.DataGridView1Pessoa.DataBindings.Add("DataSource",this, "ListPessoas");
this.DataGridView1Pessoa.AutoGenerateColumns = false;
and now how do I display Table Endereco
and the Table person in Datagridview, since I already tried to add the table Endereco
using the same code cited above, but displays the message:
"This generates two associations in the collection to link to the same property. Parameter name: Binding".
I have to display the result of this select below on dgv-DataGridView
.
"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"
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 property "query.Connection" to make the connection with BD, but I have done several searches and I could not find what I need to do to make this connection work in my application.
To display the Table person in dgv I use the connection with 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_person class inside the Date Folder. Where it contains the Get and Set. I use a Personal Model 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 BD 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 Table Person to the dgv.
public FrmCadastrarPessoa()
{
InitializeComponent();
this.DataGridView1Pessoa.DataBindings.Add("DataSource", this, "ListPessoas");
this.DataGridView1Pessoa.AutoGenerateColumns = false;
}
The problem now is how to implement the property query.Connection
of my method ListarPessoas()
and display the return of the same in dgv
?
I think the question is not so much supports more than one table, but yes if you did the select and mapping correctly, something else your relationship is 1 p/ 1 or is 1 p/ many, if it is the last case maybe it is better to create a Datagridview to open with the address data of each person.
– Marco Souza
being a table, or you display only one address of the person, or you display multiple rows of the same person with their respective addresses. Usually there is a default address of the person that is displayed in these cases, but anyway, to display in datagridview, just select with Join. I would do as colleague Marconcilio spoke, another grid to show addresses.
– Rovann Linhalis
you can edit the question by adding more information, and by your select, the resulting table already contains the address data. If dgv is autogeneraterows, it should already be displayed, otherwise just add the desired columns
– Rovann Linhalis
Welcome to Stackoverflow! Please explain the problem better, and if possible include a example of code that reproduces what is happening, because your question is not noticeable. See Help Center How to Ask.
– Renato Junior