Search code examples
delphidbgrid

How to refence a DBGrid from another form


I have a mainForm with a DBGrid and I have a second form with a CheckListBox that shows all of the DBGrid columns for the user to choose. I need to reference in Form2 the DBGrid that I have in MainForm. I would like this second form to handle all of the procedures connected to the dbdgrid columns , so that I can reuse it easily. That was the idea, but I dod'nt find the way to pass the DBGrid reference. Is it possible ?


Solution

  • Answering the question you asked, on your Form2, define a property

    TForm2 
    
    [...]
    private
      FGrid : TDBGrid
    public
      property Grid : TDBGrid read FGrid write FGrid;
    

    Then, after you've created an instance of TForm2, just do

    Form2.Grid := MainForm.DBGrid1;
    

    Then, on Form2, you can do anything valid you like to change Grid and the changes will be made to MainForm.DBGrid1.