Eingabefelder über DBGrind-Spalten positionieren



  • Hallo zusammen 😉
    Ich habe ein Problem bei dem ich selber nicht mehr weiter komme aber vielleicht hast du ja eine Idee!?

    Problem: Ich habe eine DBGrid-Komponente und mehrere Eingabefelder. Die Anzahl der Eingabefelder entspricht der Anzahl der Spalten in der DBGrid. Nun möchte ich gerne erreichen dass die Eingabefelder genau über den Spalten der DBGrid sind, kriege es aber alleine nicht hin 😢 Sobald die DBGrid eine Laufleiste hat werden die Eingabefelder falsch positioniert.

    Hast du vielleicht einen Lösungsansatz wie man das Problem aus der Welt schaffen könnte?

    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
    {
    
        EditsScrollBox = new TScrollBox(this);
        EditsScrollBox->Align = alTop;
        EditsScrollBox->Parent = this;
    
        ObjectList = new TObjectList();
    
        TableBiolife = new TTable(this);
        TableBiolife->DatabaseName = "BCDEMOS";
        TableBiolife->TableName = "biolife.db";
        TableBiolife->Active = true;
    
        DataSourceBiolife = new TDataSource(this);
        DataSourceBiolife->DataSet = TableBiolife;
    
        DBGridBiolife = new TDBGrid(this);
        DBGridBiolife->Parent = this;
        DBGridBiolife->Align = alClient;
        DBGridBiolife->DataSource =  DataSourceBiolife;
        DBGridBiolife->OnDrawColumnCell =  OnDrawColumnCell;
    
        for(int i=0; i<DBGridBiolife->Columns->Count; i++)
        {
            TColumn* ColumnFound = DBGridBiolife->Columns->Items[i];
            if(!ColumnFound)
                return;
    
            TEdit* NewEdit = new TEdit(this);
            if(!NewEdit)
                return;
    
            NewEdit->Width = ColumnFound->Width;
            NewEdit->Parent = EditsScrollBox;
            NewEdit->Hint = ColumnFound->Title->Caption;
            NewEdit->ShowHint = true;
            ObjectList->Add(NewEdit);
        }
    }
    //---------------------------------------------------------------------------
    __fastcall TForm1::~TForm1()
    {
        if(ObjectList)
            delete ObjectList;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::OnDrawColumnCell(TObject *Sender,
          const TRect &Rect, int DataCol, TColumn *Column,
          TGridDrawState State)
    {
        for(int i=0;i<ObjectList->Count; i++)
        {
            TEdit* EditFound =  dynamic_cast<TEdit*>((TObject*)ObjectList->Items[i]);
            if(!EditFound || !Column)
                return;
    
            if(EditFound->Hint == Column->Title->Caption)
            {
               EditFound->Left = Rect.Left;
               EditFound->Width = Rect.Width();
            }
    
        }
    
    }
    //---------------------------------------------------------------------------
    
    //---------------------------------------------------------------------------
    
    #ifndef Unit1H
    #define Unit1H
    //---------------------------------------------------------------------------
    #include <Classes.hpp>
    #include <Controls.hpp>
    #include <StdCtrls.hpp>
    #include <Forms.hpp>
    #include <DB.hpp>
    #include <DBGrids.hpp>
    #include <DBTables.hpp>
    #include <Grids.hpp>
    //---------------------------------------------------------------------------
    class TForm1 : public TForm
    {
    __published:
    
    private:
        TScrollBox* EditsScrollBox;
    
        TObjectList* ObjectList;
        TTable* TableBiolife;
        TDataSource* DataSourceBiolife;
        TDBGrid* DBGridBiolife;
    
        void __fastcall OnDrawColumnCell(TObject *Sender,const TRect &Rect, int DataCol, TColumn *Column, TGridDrawState State);
    
    public:
        __fastcall TForm1(TComponent* Owner);
        __fastcall ~TForm1();
    };
    //---------------------------------------------------------------------------
    extern PACKAGE TForm1 *Form1;
    //---------------------------------------------------------------------------
    #endif
    

    Wenn du sehen willst wie es momentan aussieht, dann kopiere einfach die Sourcen in ein neues Borland c++ Project.


Anmelden zum Antworten