Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Error "Grid exceded index" en stringgrid (https://www.clubdelphi.com/foros/showthread.php?t=91979)

angelratza 16-06-2017 23:45:58

Error "Grid exceded index" en stringgrid
 
tengo un problema similar y con sus experiencia de que manera creen que podría lograrlo.

Tengo este código y al utilizar el IndexOf este me manda un error "Grid exceded index"

Código Delphi [-]
texto:=FVenta.EdVenta.Text;

IF GetTokenCount(texto,'%') = 2 then //cuando hay 2 tokens con signo de %
          begin
            if (EsStrunNumero(GetToken(texto,'%',1))) and (EsStrunNumero(GetToken(texto,'%',2)))then
            begin
              idesc:= StrToInt(GetToken(texto,'%',1));//Con esta funcion se obtiene el 1er. token que es el porcentaje de descuento y la colocamos a iDesc
              scve:= GetToken(texto,'%',2);   //Con esta Funcion se obtiene el 2o. token que es la clave de producto y se deposita en scve
              Fventa.SGVenta.Row := Fventa.SGVenta.Cols[1].indexOf(scve); // Con esta funcion nos posicionamos en la fila del producto

                MessageDlg(scve, mtInformation,[mbOk], 0);
                MessageDlg(Fventa.SGVenta.Cells[0,Fventa.SGVenta.Row], mtInformation,[mbOk], 0);

              if scve = Fventa.SGVenta.Cells[0,Fventa.SGVenta.Row] then // Verificamos que sea el producto seleccionado
              begin
                importdesc:=StrToFloat(Fventa.SGVenta.Cells[6,Fventa.SGVenta.Row]); // Obtenemos el valor del importe sin descuento
                if idesc <> 0 then
                begin
                  Desc:=1-idesc/100; //a 1 se resta el porcentaje de descuento para q Desc se multiplique por la cantidad
                  import:=importdesc*Desc;
                  Fventa.SGVenta.Cells[5,Fventa.SGVenta.Row]:= IntToStr(idesc); //Descuento
                end;

                import2:=FormatFloat('#,##0.00',DM1.QDist.FieldByName('Precio').AsFloat);//a Precio se le da formato
                Fventa.SGVenta.Cells[3,Fventa.SGVenta.Row]:= import2; //Precio

                import2:=FormatFloat('#,##0.00',import);
                Fventa.SGVenta.Cells[6,Fventa.SGVenta.Row]:= import2; //Importe

                if Fventa.EdImporte.Text <> '' then
                begin
                  importotal:=StrToFloat(QuitaEn(Fventa.EdImporte.Text, ','));
                  importotal:=importotal+import;
                  import2:=FormatFloat('#,##0.00',importotal);
                  Fventa.EdImporte.Text:=import2;
                  If Fventa.EdCostoEnvio.Text = '' then
                    Fventa.EdTotal.Text:=import2
                  else
                    Fventa.EdTotal.Text:=FormatFloat('#,##0.00',importotal+StrToFloat(Fventa.EdCostoEnvio.Text));
                end
                else
                begin
                  Fventa.EdImporte.Text:=import2;
                  If Fventa.EdCostoEnvio.Text = '' then
                    Fventa.EdTotal.Text:=import2
                  Else
                    Fventa.EdTotal.Text:=FormatFloat('#,##0.00',StrToFloat(import2)+StrToFloat(Fventa.EdCostoEnvio.Text)  );
                end;

                MessageDlg(FVenta.EdVenta.Text, mtInformation,[mbOk], 0);
                Fventa.EdVenta.Clear;

              end
              Else
              begin
                MessageDlg('El PRODUCTO solicitado no ha sido todavia ingresado', mtError,[mbOk], 0);
                FVenta.EdVenta.Clear;
              end;
            end
            Else
            begin
              MessageDlg('Revise su SINTAXIS', mtError,[mbOk], 0);
              FVenta.EdVenta.Clear;
            end;
          end;

Lo que se quiere lograr es encontrar en una columna un string especifico y obtener su indice para realizar las operaciones y sustituir valores.
Saludos.

Casimiro Notevi 17-06-2017 00:34:43

He movido tu pregunta a un nuevo hilo porque no tenía nada que ver con el hilo donde lo pusiste.

ecfisa 17-06-2017 09:20:21

Hola.

En tu código, el único lugar donde usas la función IndexOf es en la línea:
Código Delphi [-]
...
  Fventa.SGVenta.Row := Fventa.SGVenta.Cols[1].indexOf(scve);
...
Pero... el error no puede provocarlo la función IndexOf ya que ella devuelve la posición en base cero, o -1 de no encontrar una ocurrencia. Por lo tanto el error debe ser provocado por el valor del índice usado en el TStrings Cols.

Para verificar (y visualizar) la validez del índice agrega lo siguiente:
Código Delphi [-]
// {$C+}
...
var
  inx: Integer;
begin
  inx := 1; 

  texto:=FVenta.EdVenta.Text;

  IF GetTokenCount(texto,'%') = 2 then
  begin
    if (EsStrunNumero(GetToken(texto,'%',1))) and (EsStrunNumero(GetToken(texto,'%',2)))then
    begin
      idesc:= StrToInt(GetToken(texto,'%',1));
      scve:= GetToken(texto,'%',2);

      if inx > SGVenta.ColCount-1 then
        raise Exception.CreateFmt('¡ Indice %d fuera de rango !%sSólo existen %d columnas',
                                  [inx, #13#10, SGVenta.ColCount-1]);
    
      Fventa.SGVenta.Row := Fventa.SGVenta.Cols[inx].indexOf(scve); 
  ...

Saludos :)

angelratza 20-06-2017 18:16:42

:DMuchas gracias ECFISA, tenias razón estaba seleccionando mal la columna al especificarla en la propiedad cols.

Finalmente quedo así y funcionando.

Código Delphi [-]
Fventa.SGVenta.Row := Fventa.SGVenta.Cols[0].indexOf(scve);

Pues resulta que la columna en donde estaba buscando era la de cantidades (que es la columna numero 1) y no la de productos (que es la columna numero 0).

Por lo tanto al no encontrar el valor en dicha columna arroja el error "grid index out of range".:D

bulc 10-04-2021 18:01:36

Error Grid Out of range
 
El error que yo tenía lo solvente añadiendo:
DataSource1.DataSet := Nil;
DataSource1.DataSet := FDQuery1;
para abrir y cerrar el Dataset.


La franja horaria es GMT +2. Ahora son las 14:43:48.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi