Difficulty to popular the Gridview

Asked

Viewed 228 times

1

I have a screen that I consult clients and fill a grid with the information coming from the bank, my project is separated by layers until where I am works, but I can not fill the grid with some information, I have the following situation within the for each:

 Cliente cliente = new Cliente();

 cliente.Pessoa = new Pessoa();
 cliente.Pessoa.IdPessoa = Convert.ToInt32(linha["IdPessoaCliente"]);
 cliente.Pessoa.Nome = Convert.ToString(linha["Nome"]);
 cliente.DataCadastro = Convert.ToDateTime(linha["DataCadastro"]); 
 cliente.DataAlteracao = Convert.ToDateTime(linha["DataAlteracao"]);
 cliente.LimiteCompra = Convert.ToDecimal(linha["LimiteCompra"]);
 cliente.Ativo = Convert.ToBoolean(linha["Ativo"]);

 cliente.Pessoa.PessoaTipo = new PessoaTipo();
 cliente.Pessoa.PessoaTipo.IdPessoaTipo = Convert.ToInt32(linha["IdPessoaTipo"]);
 cliente.Pessoa.PessoaTipo.Descricao = Convert.ToString(linha["Descricao"]);

 clienteColecao.Add(cliente);

The fields of grid that comes directly from client as for example cliente.DataCadastro carries normal, but what is cliente.Pessoa.IdPessoa in the grid appears written "transfer object", or if I customize the grid nothing comes and when threshing I see that cliente.Pessoa.IdPessoa the IdPessoa has the correct value but Pessoa is written Objeto transfer exactly what goes to the grid.

  • This "Gridview" you mention, in Winforms, you want to refer to "Datagridview"?

  • That’s right, Metalus.

3 answers

1

See if the example below helps in anything:

public void MostrarClientes()
{
        string StrCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Desktop\\Oi\\Testando.accdb";
        string sql = "SELECT * FROM Cliente";

        OleDbConnection Con = new OleDbConnection(StrCon);
        OleDbDataAdapter da = new OleDbDataAdapter(sql, Con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        dataGridView1.DataSource = dt;
}

Another example:

protected void CarregarDataGridView(String parametro)
{
  MySqlConnection conn = new MySqlConnection("Server=NomeServidor;Database=Teste;Uid=root;Pwd=sua_senha;");
  conn.Open();

  try
  {
    MySqlCommand cmd = new MySqlCommand("SELECT * FROM TABELA WHERE CAMPO=@CAMPO", conn);
    cmd.Parameters.Add(new MySqlParameter("@CAMPO", parametro));

    MySqlDataReader reader = cmd.ExecuteReader();
    DataTable table = new DataTable();

    table.Load(reader);

    this.dataGridView1.DataSource = table;

    table.Dispose();
    reader.Close();
    reader.Dispose();

  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message);
  }
  finally
  {
    conn.Close();
    conn.Dispose();
  }
}

1

You are populating your Datagridview ?

Datagridview has a property called Datasource. Here you get the source of your dice.

It would be something like:

dgvClientes.DataSource = cliente.Listagem();

Check:

Datagridview - MSDN

1

The Client.Person will not appear because there is no representation of the class objects. In simplistic terms the objects in the cells show the result of Tostring().

The question you can ask is what did you expect the Person class to show in the cell?

  • Paulo Soares, what was expected was that the class Pessoa showed in the cell the Idpessoa, I found that client.Person.Idpessoa was similar to a mathematical sequence "A = B = C = 5", where all values assume the value "5"then in case Idpessoa = 5 the result loaded in client.Person.Idpessoa = 5.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.