Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Naming Columns in a grid control
Message
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00755457
Message ID:
00755573
Views:
8
John,

>Actually, the recordsourcetype is 4 (SQL). Will that be a problem?

It's probably better to just do this at design time, it makes the grid much easier to work with IMHO. Run the SQL statement to create at least the cursor structure (you can use a where clause of where 1=2 then you can use a builder like this to rename the columns and it sets the header caption from the dbc:
* ColName.prg
lparameter plForceHeader

if ( pcount() = 0 )
   plForceHeader = .f.
endif

if ( type( "plForceHeader" ) != "L" ) 
   plForceHeader = .f.
endif

local n, i

n = aselobj( laJunk )
if ( ( n != 1 ) or ( laJunk[1].BaseClass != "Grid" ) )
   wait window nowait "Grid is not selected"
   return
endif

atable()

local lcName, lcCaption

for each oCol in laJunk[1].Columns
   i = at( ".", oCol.ControlSource )
   if ( i > 0 )
      lcName = dbcGetProperFieldName( oCol.ControlSource )
      if ( ! empty( lcName ) )
         oCol.Name = "col" + lcName
      endif
      if ( ( lower( oCol.Header1.Caption ) == oCol.Header1.Caption ) or ;
           ( oCol.Header1.Caption == "Header1" ) or ;
           empty( oCol.Header1.Caption ) or;
           plForceHeader )
         lcCaption = dbcGetCaption( oCol.ControlSource )
         if ( ! empty( lcCaption ) )
            oCol.Header1.Caption = lcCaption
         endif
      endif
   endif
endfor

* dbgGetCaption.prg
lparameter pcFieldName

local i, lcRetVal

if ( type( "gaFields[1,1]" ) != "C" )
   atable()
endif

pcFieldName = lower( alltrim( pcFieldName ) )

if ( at( '.', pcFieldName ) = 0 )
   iCol = 1
else
   iCol = 2
endif

lcRetVal = ""

for i = 1 to alen( gaFields, 1 )
   if ( gaFields[i,iCol] == pcFieldName )
      lcRetVal = alltrim( gaFields[i,4] )
      exit
   endif
endfor

return lcRetVal


* dbcGetProperFieldname.prg
lparameter pcFieldName

if ( pcount() = 0 )
   wait window "Hey don't call this directly"
endif

local i, j, iCol, lcRetVal

if ( type( "gaFields[1,1]" ) != "C" )
   atable()
endif

pcFieldName = lower( alltrim( pcFieldName ) )

if ( at( '.', pcFieldName ) = 0 )
   iCol = 1
else
   iCol = 2
endif

lcRetVal = ""

for i = 1 to alen( gaFields, 1 )
   if ( gaFields[i,iCol] == pcFieldName )
      j = at( ' -', gaFields[i,3] )
      if ( j > 0 )
         lcRetVal = left( gaFields[i,3], j-1 )
      endif
      exit
   endif
endfor

return lcRetVal

* atable.prg
lcDBC = set( "database" )
if ( empty( lcDBC ) )
   wait window "No database set, open the database"
   open database ?
   lcDBC = set( "database" )
   if ( empty( lcDBC ) )
      return .f.
   endif
endif

lcDBC = lcDBC

release gaTables, gaFields, gaViews
public gaTables[1], gaFields[1], gaViews[1]

select ObjectName ;
   from (lcDBC) ;
   into array gaTables ;
   where ObjectType = "Table" and ! deleted()

for i = 1 to _tally
   gaTables[i] = alltrim( gaTables[i] )
endfor

select ObjectName ;
   from (lcDBC) ;
   into array gaViews ;
   where ObjectType = "View" and ! deleted()

for i = 1 to _tally
   gaViews[i] = alltrim( gaViews[i] )
endfor

select DBC.ObjectName, Tables.ObjectName, "", "" ;
   from (lcDBC) as DBC;
   inner join (lcDBC) as Tables ;
      on dbc.ParentID = tables.ObjectID ;
   into array gaFields ;
   where ( DBC.ObjectType = "Field" ) and ! deleted() && and tables.ObjectType = "Table"

for i = 1 to _tally
   gaFields[i,1] = alltrim( gaFields[i,1] )
   gaFields[i,2] = alltrim( gaFields[i,2] ) + "." + gaFields[i,1]

   gaFields[i,3] = dbgetprop( gaFields[i,2], "Field", "Comment" )
   if ( empty( gaFields[i,3] ) )
      gaFields[i,3] = left( gaFields[i,1], 1 ) + proper( substr( gaFields[i,1], 2 ) )
      dbsetprop( gaFields[i,2], "Field", "Comment", gaFields[i,3] + " - " )
   else
      if ( at( " -", gaFields[i,3] ) = 0 )
         gaFields[i,3] = gaFields[i,3] + " -"
      endif
   endif

   gaFields[i,4] = dbgetprop( gaFields[i,2], "Field", "Caption" )
   if ( empty( gaFields[i,4] ) )
      gaFields[i,4] = proper( substr( gaFields[i,1], 2 ) )
      if ( left( gaFields[i,1], 1 ) == "d" )
         gaFields[i,4] = gaFields[i,4] + " Date"
      endif
      dbsetprop( gaFields[i,2], "Field", "Caption", gaFields[i,4] )
   endif
endfor

return .t.
df (was a 10 time MVP)

df FoxPro website
FoxPro Wiki site online, editable knowledgebase
Previous
Reply
Map
View

Click here to load this message in the networking platform