Foros del Web

Foros del Web (http://www.forosdelweb.com/)
-   .NET (http://www.forosdelweb.com/f29/)
-   -   Recorrer ListBox (http://www.forosdelweb.com/f29/recorrer-listbox-478210/)

jhonwilliams 02/04/2007 12:51

Recorrer ListBox
 
Hola

Estoy tratando de recorrer un ListBox que tiene como origen de datos un DataView, el problema que tengo es que al recorrerla no alcanzo el valor del item como tal, lo que alcanzo es el tipo de valor, el ListBox le asigno los datos asi:

Código:

dvLotesPOY = dsLlenarDatos.Tables[1].DefaultView;//Instancia del DataView
lstLotesPOY.DataSource = dvLotesPOY.Table;
lstLotesPOY.DisplayMember = "LotePoyProducto";
lstLotesPOY.ValueMember = "LotePoyProducto";

Asi estoy tratando de leerlo.

Código:

for(int i..............................)
{
    Variable = ListBox.Items[i].ToString();
}

El problema surgue porque a Variable no se le lleva el item seleccionado, se le lleva esto: System.Data.DataRowView

Los items que tiene el list son algo como esto:
222 PES SM RD PY 320F36 HD TX
224 PES SM RD PY 130F48 HD TX
225 PES SM RD PY 180F48 HD TX
.
.
.
Y es eso lo que necestio leer.

Gracias
:adios:

RootK 02/04/2007 14:41

Re: Recorrer ListBox
 
inténtalo de ésta forma:

Código:

  System.Data.DataRowView dr = null;
            for (int i = 0; i<=ListBox.Items.Count -1; i++) {
                dr = (System.Data.DataRowView)ListBox.Items[i];

                MessageBox.Show(dr["Columna"].ToString());
            }

Salu2

jhonwilliams 02/04/2007 15:05

Re: Recorrer ListBox
 
Hola RootK, te agradesco mucho tu ayuda pero ya me enrrede, te posteo el code que tengo completo y tratare de explicartelo mas detenidamente:

Yo no nesito recorrer todos los items, solo los que estan seleccionados, eso fue algo que se me olvido escribir en el mensaje anterior, para ello uso un codigo como este:

Código:

string LotesPoy = "";//Variable para items seleccinados
if (lstLotesPOY.SelectedItems.Count > 0)//Pregunto si hay items seleccionados
{
      LotesPoy = "('";
      for (int i = 0; i < lstLotesPOY.Items.Count; i++)
      {
            if (lstLotesPOY.GetSelected(i) == true)//Esto lo use para ver si el item en la posicion i esta seleccionado
              {
                    LotesPoy = LotesPoy + lstLotesPOY.Items[i].ToString().Substring(0, 3) + "', '";
                }
            }
                LotesPoy = LotesPoy.Substring(0, LotesPoy.Length - 3) + ")";
      }
return LotesPoy;

El string que se retorna es algo como esto

('222','224','256'....)

Solo los 3 primeros dijitos de cada item seleccionado se agrega al string.

RootK 02/04/2007 15:21

Re: Recorrer ListBox
 
y así no te sirve:

Código:

string LotesPoy = "";//Variable para items seleccinados
System.Data.DataRowView dr = null;
if (lstLotesPOY.SelectedItems.Count > 0)//Pregunto si hay items seleccionados
{
      LotesPoy = "('";
      for (int i = 0; i < lstLotesPOY.Items.Count; i++)
      {
            if (lstLotesPOY.GetSelected(i) == true)//Esto lo use para ver si el item en la posicion i esta seleccionado
              {
dr = (System.Data.DataRowView)lstLotesPOY.Items[i];
                    LotesPoy = LotesPoy + dr["Columna"].ToString().Substring(0, 3) + "', '";
                }
            }
                LotesPoy = LotesPoy.Substring(0, LotesPoy.Length - 3) + ")";
      }
return LotesPoy;


jhonwilliams 02/04/2007 15:41

Re: Recorrer ListBox
 
RootK gracias por tu ayuda el codigo que acabas de poner no lo revise, :-) entre para decirte que el code quedo asi:

Código:

string LotesPoy = "";
            if (lstLotesPOY.SelectedItems.Count > 0)
            {
                LotesPoy = "('";
                foreach (DataRowView r in lstLotesPOY.SelectedItems)
                {
                    LotesPoy = LotesPoy + r.Row[0].ToString().Substring(0, 3) + "', '";
                }
                LotesPoy = LotesPoy.Substring(0, LotesPoy.Length - 3) + ")";
            }

Nuevamente muchas gracias por tu ayuda.

Saludos
:adios:


La zona horaria es GMT -6. Ahora son las 07:39.

Desarrollado por vBulletin® Versión 3.8.7
Derechos de Autor ©2000 - 2026, Jelsoft Enterprises Ltd.