none
List<string> llena Gridview muestra datos en una sola columna RRS feed

  • Question

  • Pues sucede que estoy llenando un Gridview desde un arreglo asi:

    public List<String> lista()
        {
            List<string> listadocentes = new List<string>();
    
            cn.Open();
            cmd.Connection = cn;
            cmd.CommandText = "select distinct(asis_doc_cod_docente) from pla_asis_doc";
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                //listadocentes.Add(dr.GetInt32(0).ToString());
                int codigo = int.Parse(dr.GetInt32(0).ToString());
                SqlCommand cmd2 = new SqlCommand();
                cmd2.Connection = cn;
                cmd2.CommandText = "select emp_codigo, emp_nombres_apellidos from pla_emp_empleado where emp_codigo = '" + codigo + "'";
                SqlDataReader dr2 = cmd2.ExecuteReader();
                while (dr2.Read())
                {
                    listadocentes.Add(dr2.GetInt32(0).ToString());
                    listadocentes.Add(dr2.GetString(1).ToString());
                    
                }
            }
            cn.Close();
            return listadocentes;
        }
    

    y el gridview lo recibe en esta porcion de codigo:

    public void llenar_grid2()
        {
            List<String> listado = puente.lista();
            GridView2.DataSource = listado;
            GridView2.DataBind();
        }

    el metodo llenar_grid2(){} se ejecuta al cargar la pagina. Mi problema es que al mostrar los datos de ese arreglos (que son dos: emp_codigo, emp_nombres_apellidos) me muestra los datos en una sola columna apilados y lo que necestio es que se muestren en columnas diferentes. Alguien que ayude porfavor!

    • Moved by Eyal Solnik Wednesday, May 13, 2015 9:55 AM foreign language
    Wednesday, April 23, 2014 3:28 PM

Answers

  • Ok, here is the code:

    First, add an auxiliary class somewhere convenient, for instance, it can be nested inside the same class that contains your lista() and llenar_grid2() methods:

    private class CodigoYNombre
    {
        public string Codigo { get; set; }
        public string NombreApellidos{ get; set; }
    }

    Second, modify your lista() method:

        public List<CodigoYNombre> lista()
        {
            List<CodigoYNombre> listadocentes = new List<CodigoYNombre>();
    
            cn.Open();
            cmd.Connection = cn;
            cmd.CommandText = "select distinct(asis_doc_cod_docente) from pla_asis_doc";
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                //listadocentes.Add(dr.GetInt32(0).ToString());
                int codigo = int.Parse(dr.GetInt32(0).ToString());
                SqlCommand cmd2 = new SqlCommand();
                cmd2.Connection = cn;
                cmd2.CommandText = "select emp_codigo, emp_nombres_apellidos from pla_emp_empleado where emp_codigo = '" + codigo + "'";
                SqlDataReader dr2 = cmd2.ExecuteReader();
                while (dr2.Read())
                {
                    string codigo = dr2.GetInt32(0).ToString();
                    string nombreApellidos = dr2.GetString(1).ToString();
                    CodigoYNombre cn = new CodigoYNombre { Codigo=codigo, NombreApellidos=nombreApellidos };
                    listadocentes.Add(cn);
                }
            }
            cn.Close();
            return listadocentes;
        }

    Third, change the type of List in the databinding:

        public void llenar_grid2()
        {
            List<CodigoYNombre> listado = puente.lista();
            GridView2.DataSource = listado;
            GridView2.DataBind();
        }

    This should produce a two-column grid as you desired.

    BTW, it is a good practice to close the datareader before you close the connection (add dr2.Close()).

    • Proposed as answer by Dany Tapia Thursday, April 24, 2014 12:05 AM
    • Marked as answer by Alejo_Blue Friday, April 25, 2014 8:35 PM
    Wednesday, April 23, 2014 5:30 PM

All replies

  • First of all, if you want to write your question in Spanish, please use the Spanish C# forum:

    http://social.msdn.microsoft.com/Forums/es-ES/home?forum=vcses

    Second, in order to solve your problem, instead of a List<string> you should use a List<MyClass>, where MyClass would be a small private class with two properties (codigo and nombres_apellidos). Inside your lista() method, each iteration of the loop would add to the list a single new instance of MyClass and store inside the two values. When binding this list to the GridView, the results will appear in two columns as you wish.

    If the preceding doees not solve your question, and you need a code example, please ask in the Spanish forum linked above. I am a moderator there and I will make sure that you get an answer. -- Si lo anterior no resuelve tu problema y necesitas un ejemplo de código, plantea la pregunta en el foro español enlazado más arriba.

    Wednesday, April 23, 2014 4:36 PM
  • I didn't realize i had posted my question in a english forum, sorry about that :( . If you could provide me with a simple example i'd be more than thankful!!!! 
    Wednesday, April 23, 2014 4:44 PM
  • Ok, here is the code:

    First, add an auxiliary class somewhere convenient, for instance, it can be nested inside the same class that contains your lista() and llenar_grid2() methods:

    private class CodigoYNombre
    {
        public string Codigo { get; set; }
        public string NombreApellidos{ get; set; }
    }

    Second, modify your lista() method:

        public List<CodigoYNombre> lista()
        {
            List<CodigoYNombre> listadocentes = new List<CodigoYNombre>();
    
            cn.Open();
            cmd.Connection = cn;
            cmd.CommandText = "select distinct(asis_doc_cod_docente) from pla_asis_doc";
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                //listadocentes.Add(dr.GetInt32(0).ToString());
                int codigo = int.Parse(dr.GetInt32(0).ToString());
                SqlCommand cmd2 = new SqlCommand();
                cmd2.Connection = cn;
                cmd2.CommandText = "select emp_codigo, emp_nombres_apellidos from pla_emp_empleado where emp_codigo = '" + codigo + "'";
                SqlDataReader dr2 = cmd2.ExecuteReader();
                while (dr2.Read())
                {
                    string codigo = dr2.GetInt32(0).ToString();
                    string nombreApellidos = dr2.GetString(1).ToString();
                    CodigoYNombre cn = new CodigoYNombre { Codigo=codigo, NombreApellidos=nombreApellidos };
                    listadocentes.Add(cn);
                }
            }
            cn.Close();
            return listadocentes;
        }

    Third, change the type of List in the databinding:

        public void llenar_grid2()
        {
            List<CodigoYNombre> listado = puente.lista();
            GridView2.DataSource = listado;
            GridView2.DataBind();
        }

    This should produce a two-column grid as you desired.

    BTW, it is a good practice to close the datareader before you close the connection (add dr2.Close()).

    • Proposed as answer by Dany Tapia Thursday, April 24, 2014 12:05 AM
    • Marked as answer by Alejo_Blue Friday, April 25, 2014 8:35 PM
    Wednesday, April 23, 2014 5:30 PM
  • Thank you very much!! It helped indeed!!! (Y) 
    Friday, April 25, 2014 8:36 PM