Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Firebird e Interbase (https://www.clubdelphi.com/foros/forumdisplay.php?f=19)
-   -   Checkbox en un DBGrid (https://www.clubdelphi.com/foros/showthread.php?t=90778)

Angel.Matilla 02-09-2016 11:09:23

Checkbox en un DBGrid
 
Buenas a todos. A ver si me podéis ayudar que no sé por dónde salir. La aplicaicón está hecah con Builder 6 y tengo este DBGrid cargado a partir de una tabla en FB, por lo tanto no tiene campos tipo BOOL y uso uno SMALLINT, en el que he conseguido meter un Checkbox cogiendo trozos de código.

Y tengo este código:
Código:

void __fastcall TfAboRec::DBGrid1DrawColumnCell(TObject *Sender,
      const TRect &Rect, int DataCol, TColumn *Column,
      TGridDrawState State)
{
    TCanvas *c = DBGrid1->Canvas;
    if (Column->FieldName == "SELECCION")
          DibujarCheckBox(c, Rect, Column->Field->AsInteger);
    else
          DBGrid1->DefaultDrawColumnCell(Rect, DataCol, Column, State);
}
//---------------------------------------------------------------------------

void __fastcall TfAboRec::DibujarCheckBox(TCanvas *c, const TRect &Rect, int Valor)
{
    TRect r = Rect;
    UINT Checked = (!Valor ? DFCS_BUTTONCHECK : DFCS_BUTTONCHECK | DFCS_CHECKED);
    c->FillRect(Rect);
    r.Top += 2;
    r.Bottom -= 1;
    r.Left += 1;
    r.Right -= 1;
    DrawFrameControl(c->Handle, (RECT*)&r, DFC_BUTTON, DFCS_BUTTONCHECK | Checked);
}
//---------------------------------------------------------------------------

void __fastcall TfAboRec::DBGrid1CellClick(TColumn *Column)
{
    int nValor;

    if (Column->FieldName == "SELECCION")
    {
          nValor = Column->Field->AsInteger;

          fPersona->tTemp->Edit();
          fPersona->tTemp->FieldValues[Column->FieldName] = !nValor;
          fPersona->tTemp->Post();
    }
}
//---------------------------------------------------------------------------

Se compila y ejecuta sin ningún problema y el checkbox se marca y desmarca bien, pero a la hora de actualizar el valor de la tabla en función de si está o no marcado el Checkbox no me modificac nada y no sé como arreglarlo.

Angel.Matilla 02-09-2016 11:41:59

Por cierto, que se me olvidó: La tabla que alimenta al DBGrid está definida así:
Código SQL [-]
CREATE TABLE Tabla (Identidad VARCHAR(60), Referencia VARCHAR(13), Codigo INTEGER, 
Importe DECIMAL(9,2), Seleccion SMALLINT DEFAULT 0, PRIMARY KEY (Identidad, Referencia, Codigo))

Casimiro Notevi 02-09-2016 18:45:08

Cita:

Empezado por Angel.Matilla (Mensaje 508395)
... a la hora de actualizar el valor de la tabla en función de si está o no marcado el Checkbox no me modificac nada y no sé como arreglarlo.

¿Dónde está el update?

ecfisa 02-09-2016 20:21:36

Hola.

De este modo tiene que funcionarte:
Código PHP:

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  
TDBCheckBox *CB static_cast<TDBCheckBox*>(DBCheckBox1);
  
CB->Caption    "";
  
CB->ValueChecked 1;
  
CB->ValueUnchecked 0;
  
CB->DataSource DataSource1;
  
CB->DataField  "SELECCION";
  
CB->Visible    false;
}


void __fastcall TForm1::DBGrid1DrawColumnCell(TObject *Sender,
      const 
TRect &Rectint DataColTColumn *ColumnTGridDrawState State)
{
  const 
int IsChecked[2] = {DFCS_BUTTONCHECKDFCS_BUTTONCHECK DFCS_CHECKED};

  
TDBGrid *GR static_cast<TDBGrid*>(Sender);
  
TDBCheckBox *CB static_cast<TDBCheckBox*>(DBCheckBox1);
  
TRect DrawRect;

  if (
Column->Field->FieldName == CB->DataField) {
    if (
State.Contains(gdFocused) ) {
      
CB->Left    GR->Left Rect.left 2;
      
CB->Top     GR->Top  Rect.top 2;
      
CB->Width   Rect.Right Rect.Left;
      
CB->Height  Rect.Bottom Rect.Top;
      
CB->Visible true;
    }  else {
      
DrawRect Rect;
      
InflateRect(&DrawRect, -1, -1);
      
GR->Canvas->FillRect(Rect);
      
DrawFrameControl(GR->Canvas->Handle, &DrawRectDFC_BUTTON,
        
IsChecked[Column->Field->AsInteger]);
    }
  }
}

void __fastcall TForm1::DBGrid1ColExit(TObject *Sender)
{
  
TDBGrid *GR static_cast<TDBGrid*>(Sender);
  
DBCheckBox1->Visible GR->SelectedField->FieldName == DBCheckBox1->DataField;


(fijate que usé un TDBCheckBox en lugar de un TCheckBox)

Y en la definición de la tabla agregaría:
Código SQL [-]
CREATE TABLE TABLA (
  ...,
  SELECCION   SMALLINT DEFAULT 0 CHECK(SELECCION IN (0,1)),
  ...
);

Y, como lógicamente te indica Casimiro, tu DataSet tiene que tener la capacidad de actualizar la tabla.

Saludos :)

Angel.Matilla 05-09-2016 09:34:52

Cita:

Empezado por Casimiro Notevi (Mensaje 508412)
¿Dónde está el update?

En el código, al final:
Código:

void __fastcall TfAboRec::DBGrid1CellClick(TColumn *Column)
{
    int nValor;

    if (Column->FieldName == "SELECCION")
    {
          nValor = Column->Field->AsInteger;

          fPersona->tTemp->Edit();
          fPersona->tTemp->FieldValues[Column->FieldName] = !nValor;
          fPersona->tTemp->Post();
    }
}
//---------------------------------------------------------------------------

Después de haber subido el mensaje hice otra prueba distinta con este otro código:
Código:

void __fastcall TfAboRec::DBGrid1CellClick(TColumn *Column)
{
    int nCodigo, nValor;

    if (Column->FieldName == "SELECCION")
    {
          nValor = Column->Field->AsInteger;
          nCodigo = DBGrid1->Columns->Items[4]->Field->Value;

          Query->Close();
          Query->SQL->Text = "UPDATE " + cTabTemp + " SET Seleccion = :Seleccion WHERE Codigo = :Codigo";
          Query->ParamByName("Seleccion")->AsInteger = nValor == 0 ? 1 : 0;
          Query->ParamByName("Codigo")->AsInteger    = nCodigo;
          try
          {
              Query->ExecSQL();
              Query->Transaction->Commit();
          }
          catch(...)
          {
          }
    }
}
//---------------------------------------------------------------------------

Y sí, así me actualizó la tabla... pero ¡me dejó vacío el DBGrid! :mad:

Angel.Matilla 05-09-2016 10:08:34

Cita:

Empezado por ecfisa (Mensaje 508413)
Hola.

De este modo tiene que funcionarte:

Pues no, tampoco actualiza la tabla. Al final encontré uan forma un pelín "pedestre", pero fumciona y hasta que encuentre una mejor me vale:
Código:

void __fastcall TfAboRec::DBGrid1CellClick(TColumn *Column)
{
    int nCodigo, nValor;

    if (Column->FieldName == "SELECCION")
    {
          nValor = Column->Field->AsInteger;
          nCodigo = DBGrid1->Columns->Items[4]->Field->Value;

          fPersona->tTemp->Close();    // Cierro al tabal ANTES de actualizar
          fPersona->Query->Close();
          fPersona->Query->SQL->Text = "UPDATE " + cTabTemp + " SET Seleccion = :Seleccion WHERE Codigo = :Codigo";
          fPersona->Query->ParamByName("Seleccion")->AsInteger = nValor == 0 ? 1 : 0;
          fPersona->Query->ParamByName("Codigo")->AsInteger    = nCodigo;
          try
          {
              fPersona->Query->ExecSQL();
              fPersona->Query->Transaction->Commit();
              fPersona->tTemp->Open();    // Vuelvo a abrir la tabla
          }
          catch(...)
          {
          }
    }
}


ecfisa 05-09-2016 13:59:56

Hola Angel.Matilla.

Bueno, pero eso era debido al tipo de componente data aware que estabas usando o la forma en que lo usabas.

Varios juegos de componentes permiten asociarle a sus "queries" un componente updatesql que te evitaría escribir ese código, como por ejemplo la dupla TIBQuery/TIBUpdateSQL. Pero para ser mas específico habría que saber que componentes estas usando.

Saludos :)


La franja horaria es GMT +2. Ahora son las 11:15:57.

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