Bindingsource among more than one class

Asked

Viewed 94 times

2

I am developing a system in C# windows form where I have my classes developed according to my database. My problem is that I am not able to make my Bindingsource list in a Datagridview the query data. Follow the method that fills Bindingsource:

public List<Classes.clUsuarioSistema> ConsultaListaUsuario(int id)
        {
            try
            {
                clConexao.AbreConexao();
                MySqlCommand cmd = new MySqlCommand("SELECT * FROM USUARIOSISTEMA WHERE IDUSUARIO = @idusuario");
                cmd.Connection = clConexao.sqlConn;

                MySqlParameter parametroidUsuario = new MySqlParameter("@idusuario", MySqlDbType.Int32);
                parametroidUsuario.Value = id;
                cmd.Parameters.Add(parametroidUsuario);

                List<Classes.clUsuarioSistema> listaUsuarioSistema = new List<Classes.clUsuarioSistema>();

                MySqlDataReader drUsuarioSistema = cmd.ExecuteReader();

                while (drUsuarioSistema.Read())
                {
                    Classes.clUsuarioSistema vUsuarioSistema = new Classes.clUsuarioSistema();
                    Classes.clUsuario vUsuario = new Classes.clUsuario();
                    Classes.clSistema vSistema = new Classes.clSistema();
                    clSistemaDAO vSistemaDAO = new clSistemaDAO();
                    clUsuarioDAO vUsuarioDAO = new clUsuarioDAO();

                    vUsuario.idUsuario = Convert.ToInt32(drUsuarioSistema["IDRESPONSAVEL"].ToString());
                    vUsuario.Login = vUsuarioDAO.Consultar(vUsuario.idUsuario).Login;

                    vSistema.idSistema = Convert.ToInt32(drUsuarioSistema["IDSISTEMA"].ToString());
                    vSistema.Descricao = vSistemaDAO.Consultar(vSistema.idSistema).Descricao;

                    vUsuarioSistema.DataAtualizacao = Convert.ToDateTime(drUsuarioSistema["DATAATUALIZACAO"].ToString());
                    vUsuarioSistema.Responsavel = vUsuario;
                    vUsuarioSistema.Sistema = vSistema;

                    listaUsuarioSistema.Add(vUsuarioSistema);
                }
                return listaUsuarioSistema.ToList();
            }
            catch
            {
                clConexao.FechaConexao();
                throw;
            }
            finally
            {
                clConexao.FechaConexao();
            }
        }

Where this return I link to the form Bindingsource.

The result is as follows:

inserir a descrição da imagem aqui

What I need to do to show up on the Grid the data I need?

  • And how are you passing this data to the grid?

  • Josiah, try to override the toString of the classes clUsuario and clSistema so that they return Login and Descricao respectively.

  • Actually I’m a beginner in C#, I took an abandoned project of my father and was continuing. Suddenly the override is the solution, but I’ll have to read it.

2 answers

2

You’re passing the object and not the property.

vUsuarioSistema.Responsavel = vUsuario;
vUsuarioSistema.Sistema = vSistema;

In this case (I don’t know your class), you should assign so:

vUsuarioSistema.Responsavel = vUsuario.Nome;
vUsuarioSistema.Sistema = vSistema.Nome;

In case the Nome is the property you want to display.

As quoted by Jeférson, Closeconnection on catch is not necessary as Finally will always run independently whether an error has occurred or not.

  • it would be interesting to add in your reply that that FechaConexao within the catch is unnecessary.

  • I really appreciate your feedback. However, as described by Laerte, the system is waiting for a class and I try to pass a string. The following error appears:Error 3 Cannot implicitly Convert type 'string' to 'Richter.Classes.clUsuario'

  • How are you populating this grid? Do you just want to display a name on the grid? Why not just create the property instead of passing the entire class?

  • To popular the grid, I linked the result of the method described to a Bindingsource that in turn I linked to a bindingNavigator, then linked to the grid.. I even managed to list the data I want, but I added each column and its respective data, but bindingNavigator is not linked to grid.

0


I don’t know if this is the right way to solve this problem, but the solution I found was to make an override, as advised by Tobymosque. Thank you for your cooperation.

Browser other questions tagged

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