User Guide
154 Chapter 9 Building Dynamic Forms
Populating a grid from a query
To populate a grid from a query:
1 Open a new file named grid1.cfm in ColdFusion Studio.
2 Edit the file so that it appears as follows:
<cfquery name="empdata" datasource="CompanyInfo">
SELECT * FROM Employee
</cfquery>
<cfform name="Form1" action="submit.cfm" method="Post">
<cfgrid name="employee_grid" query="empdata"
selectmode="single">
<cfgridcolumn name="Emp_ID">
<cfgridcolumn name="LastName">
<cfgridcolumn name="Dept_ID">
</cfgrid>
<br><input type="Submit" value="Submit">
</cfform>
Note
Use the
cfgridcolumn display="No" attribute to hide columns you want to
include in the grid but not expose to an end user. You would typically use this
attribute to include columns such as the table’s primary key column in the results
returned by cfgrid without exposing this data to the user.
3 Save the file and view it in your browser.
Reviewing the code
The following table describes the highlight code and its function:
Code Description
<cfgrid name="employee_grid"
query="empdata"
Create a grid named "employee_grid" and
populate it with the results of the query
"empdata".
selectmode="single">
Allow the user to select only one cell. Other
modes are row, column and edit.
<cfgridcolumn NAME="Emp_ID">
Put the contents of the Emp_ID column in the
query results in the first column of the grid.
<cfgridcolumn
NAME="LastName">
Put the contents of the LastName column in the
query results in the second column of the grid.
<cfgridcolumn name="Dept_ID">
Put the contents of the Dept_ID column in the
query results in the third column of the grid.