How to display 2 DB tables within a DataGridView in the application?

3

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 ?

    
asked by anonymous 16.08.2017 / 13:08

1 answer

0

I had the same problem ... so I solved it like this:

I got the JSON returned from WebApi which in my case is like this:

{
    "ped_codigo": 1,
    "ped_empresa": 1,
    "emp_razao": "1-EMPRESA",
    "ped_cliente": 2,
    "pes_razao_social": "VICENTE P DOS SANTOS",
    "cid_nome": "TANGUA",
    "uf_descricao": "RIO DE JANEIRO",
    "ped_data_digitacao": "2017-07-12T00:00:00",
    "ped_data_entrega": "2017-07-12T00:00:00",
    "ped_pedido_cliente": "123456",
    "ped_pedido_representante": "12345678",
    "ped_setor": 1,
    "ped_representante": 1,
    "razaoRepresentante": "REPRESENTANTE 1"
}

Then I just made it play into the Grid DataSource in this way (use DevExpress):

gridControl.DataSource = JsonConvert.DeserializeObject<DataTable>(retorno);

Hope you can help!

    
18.08.2017 / 19:55