Subject Re: Derive grid's fieldName
From Mervyn Bick <invalid@invalid.invalid>
Date Mon, 25 Oct 2021 11:20:26 +0200
Newsgroups dbase.getting-started
Attachment(s) test_grid1.wfm

On 2021/10/25 09:31, Akshat Kapoor wrote:

> It was a narrow miss but still a miss.
> I am more surprised to the reason why I am getting variable not defined
> error in test_grid.wfm
>
> The columns have not been defined but the grid is still displaying data.
> So it is arranging data into columns.
> So the columns array must be there.

If you open your form in the designer, select the grid and click on the
I button for the columns property you will see that the size property is
0.  The form.gridn.columns['column1'].datalink object only exists if one
has used the Spanner button and then used the Column Properties Builder
dialogue form to select columns for the grid.

If the form.gridn.columns['column1'].datalink object doesn't exist it
means that a rowset, with fields determined by the query's SELECT
statement has been assigned to the grids datalink property.  In this
case the fieldname of the field number assigned to the column number is
what Tom is looking for.

    function form_onOpen()
       ?form.grid1.columncount
       try
          ?form.grid1.columns["Column1"].datalink.fieldname
       catch(exception e)
          ? form.grid1.datalink.fields[1].fieldname
       endtry
       inspect(form)
       return

A revised version of your form is attached.

Mervyn.


** END HEADER -- do not remove this line
//
// Generated on 2021-10-25
//
parameter bModal
local f
f = new test_grid1Form()
if (bModal)
   f.mdi = false // ensure not MDI
   f.readModal()
else
   f.open()
endif

class test_grid1Form of FORM
   with (this)
      onOpen = class::FORM_ONOPEN
      height = 24.4545
      left = 4.7143
      top = 0.0
      width = 146.7143
      text = ""
   endwith

   this.DBASESAMPLES1 = new DATABASE(this)
   with (this.DBASESAMPLES1)
      left = 66.0
      top = 3.0
      width = 11.0
      height = 1.0
      databaseName = "DBASESAMPLES"
      active = true
   endwith

   this.CUSTOMERS1 = new QUERY(this)
   with (this.CUSTOMERS1)
      left = 52.0
      top = 2.0
      width = 9.0
      height = 1.0
      database = form.dbasesamples1
      sql = "select * from CUSTOMERS.DBF where customerid<35"
      active = true
   endwith

   this.GRID1 = new GRID(this)
   with (this.GRID1)
      dataLink = form.customers1.rowset
      height = 7.5
      left = 10.0
      top = 5.5
      width = 133.0
   endwith

   this.rowset = this.customers1.rowset

   function form_onOpen()
      ?form.grid1.columncount
      try
         ?form.grid1.columns["Column1"].datalink.fieldname
      catch(exception e)
         ? form.grid1.datalink.fields[1].fieldname
      endtry
                inspect(form)
      return

endclass