Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 05-02-2013
steelha steelha is offline
Miembro
 
Registrado: mar 2012
Posts: 158
Poder: 13
steelha Va por buen camino
Como hacer para totalizar una columna de un stringgrid por valor

Hola foristas, la siguiente duda es acerca de trabajar con los stringgrid, me piden leer un txt el cual trae una inmencidad de datos, lo cual ya realice. Luego me piden que si puedo dar los datos mas resumidos, es decir, que por numero de autorizacion el cual se guarda en la primera columna. Al implementar este codigo el cual y segun yo no iva a tener ningun tipo de problema me encuentro con que me da error de Out of index justamente al entrar en la parte del codigo en rojo he intentado ver el porque y no he dado con el problema. utilizo Delphi 7, tadvstringgrid de los componentes Devexp.

Código Delphi [-]
procedure TForm1.Button4Click(Sender: TObject);
var
  ruta : string;
  dia  : Word;
  mes  : Word;
  ano  : Word;
  hor  : Word;
  min  : Word;
  sec  : Word;
  mil  : Word;
  i    : Integer;
  romp : string;
  total: Real;
  filaactual : integer;
begin
  DecodeDate(Now,ano,mes,Dia);
  DecodeTime(Now,hor,min,sec,mil);
  ruta := OpenDialog1.InitialDir +'Universal_'+inttostr(Dia)+inttostr(mes)+inttostr(ano)+'_'+inttostr(hor)+inttostr(min)+inttostr(sec  )+'.txt';

  i     := 0;
  total := 0;
  filaactual := 0;
  romp  := '';
  romp  := Trim(DataGrid.Cells[0,2]);
  for i := 1 to (DataGrid.RowCount-1) do
  begin
    DataGrid.Row := i; //Posicionar en 1ra fila a trabajar
    If Trim(DataGrid.cells[0,i]) <> '' then
    begin
      If Trim(DataGrid.cells[0,i]) <> Trim(romp) then
          begin
            filaactual := DataGridagrupado.RowCount;
            DataGridagrupado.Row := filaactual;
            DataGridagrupado.Cells[0,filaactual-1] := DataGrid.Cells[0,i];
            DataGridagrupado.Cells[1,filaactual-1] := DataGrid.Cells[1,i];
            DataGridagrupado.Cells[2,filaactual-1] := FloatToStr(total);
            total := 0;
            total := total + StrToFloat(DataGrid.cells[2,i]);
            romp  := Trim(DataGrid.cells[0,i]);
            DataGridagrupado.AddRow;
          end
      else
          total := total + StrToFloat(DataGrid.cells[2,i]);
    end;
  end;

  DataGridagrupado.SaveToCSV(ruta);
end;
Responder Con Cita
  #2  
Antiguo 05-02-2013
Avatar de duilioisola
[duilioisola] duilioisola is offline
Miembro Premium
 
Registrado: ago 2007
Ubicación: Barcelona, España
Posts: 1.734
Poder: 20
duilioisola Es un diamante en brutoduilioisola Es un diamante en brutoduilioisola Es un diamante en bruto
El problema parece estar en que DataGridAgrupado no está inicializado y tiene 0 filas.
Luego FilaActual = 0
En la siguiente linea le dices que guarde en la linea -1 (FilaActual-1) un valor.

Código Delphi [-]
   filaactual := DataGridagrupado.RowCount;
   DataGridagrupado.Row := filaactual;
   DataGridagrupado.Cells[0,filaactual-1] := DataGrid.Cells[0,i];
Quizás deberías mover la línea que inserta una nueva línea al principio.
Código Delphi [-]
  for i := 1 to (DataGrid.RowCount-1) do
  begin
    DataGrid.Row := i; //Posicionar en 1ra fila a trabajar
    If Trim(DataGrid.cells[0,i]) <> '' then
    begin
      If Trim(DataGrid.cells[0,i]) <> Trim(romp) then
          begin
            DataGridagrupado.AddRow;
            filaactual := DataGridagrupado.RowCount;
            DataGridagrupado.Row := filaactual;
            DataGridagrupado.Cells[0,filaactual-1] := DataGrid.Cells[0,i];
            DataGridagrupado.Cells[1,filaactual-1] := DataGrid.Cells[1,i];
            DataGridagrupado.Cells[2,filaactual-1] := FloatToStr(total);
Responder Con Cita
  #3  
Antiguo 05-02-2013
steelha steelha is offline
Miembro
 
Registrado: mar 2012
Posts: 158
Poder: 13
steelha Va por buen camino
Ok gracias probaré, pero por demas ambos stringgrid traen por default 2 filas una para la fixed row (que es el encabezado) y una fila mas sino tedeja el fixed row como si fuera una fila normal
Responder Con Cita
  #4  
Antiguo 05-02-2013
steelha steelha is offline
Miembro
 
Registrado: mar 2012
Posts: 158
Poder: 13
steelha Va por buen camino
Ya lo intente pero aun asi me dice Index out of range
Responder Con Cita
  #5  
Antiguo 05-02-2013
steelha steelha is offline
Miembro
 
Registrado: mar 2012
Posts: 158
Poder: 13
steelha Va por buen camino
Ok problema de index out range resuelto, pero aun me falta mejorar la rotura de total por el valor de la columna 0, Gracias por la ayuda

Código Delphi [-]
procedure TForm1.Button4Click(Sender: TObject);
var
  ruta : string;
  dia  : Word;
  mes  : Word;
  ano  : Word;
  hor  : Word;
  min  : Word;
  sec  : Word;
  mil  : Word;
  i    : Integer;
  romp : string;
  total: Real;
  filaactual : integer;
begin
  DecodeDate(Now,ano,mes,Dia);
  DecodeTime(Now,hor,min,sec,mil);
  ruta := OpenDialog1.InitialDir +'Universal_'+inttostr(Dia)+inttostr(mes)+inttostr(ano)+'_'+inttostr(hor)+inttostr(min)+inttostr(sec  )+'.txt';

  i     := 0;
  total := 0;
  filaactual := 0;
  romp  := '';
  romp  := Trim(DataGrid.Cells[0,2]);
  for i := 1 to (DataGrid.RowCount-1) do
  begin
    DataGrid.Row := i; //Posicionar en 1ra fila a trabajar
    If Trim(DataGrid.cells[0,i]) <> '' then
    begin
      If Trim(DataGrid.cells[0,i]) <> Trim(romp) then
          begin
            filaactual := DataGridagrupado.RowCount;
            DataGridagrupado.Row := filaactual-1;
            DataGridagrupado.Cells[0,filaactual] := DataGrid.Cells[0,i];
            DataGridagrupado.Cells[1,filaactual] := DataGrid.Cells[1,i];
            DataGridagrupado.Cells[2,filaactual] := FloatToStr(total);
            total := 0;
            total := total + StrToFloat(DataGrid.cells[2,i]);
            romp  := Trim(DataGrid.cells[0,i]);
            DataGridagrupado.AddRow;
          end
      else
          total := total + StrToFloat(DataGrid.cells[2,i]);
    end;
  end;

  DataGridagrupado.SaveToCSV(ruta);
end;
Responder Con Cita
  #6  
Antiguo 05-02-2013
steelha steelha is offline
Miembro
 
Registrado: mar 2012
Posts: 158
Poder: 13
steelha Va por buen camino
Bueno el codigo anterior no da ningun error ahora, pero tampoco me coloca nada en el grid solo aparecen una cantidad x de filas en blanco
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Hacer que una columna de un DbGrid aparezca distinta según valor Aprendiendo OOP 11 30-11-2018 14:47:20
Problemas con Totalizar una columna de un DBGrid JoAnCa Conexión con bases de datos 2 22-08-2008 15:55:20
Hacer readonly una columna en stringgrid zcatzz Varios 5 26-09-2007 16:59:50
Como hacer para que el ancho de una columna de un TListView permanezca inalterable abracadabra OOP 17 01-10-2005 00:33:35
como hacer que una columna del dbgrid contenga un valor de un TEdit en cada celda Shidalis OOP 2 02-08-2005 12:05:35


La franja horaria es GMT +2. Ahora son las 05:41:51.


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
Copyright 1996-2007 Club Delphi