Use CFGrid selectedItem as a form value

Can the selectedItem variable be refrenced in a form submission?  I am not sure if I am asking that right so here is an example:

OK, I have this grid in a form-->

<cfform action="anotherpage.cfm">
     <cfgrid name="Grid" query="Grid">
          <cfgridcolumn name="ColumnOne" header="Column One">

Can it be refrenced in the submission of that form if a value is highlighted in that grid?

<cfquery datasource="Table">
     INSERT INTO tableOne (GridItemNumber)
     VALUES ('#Form.Grid.selectedItem.ColumnOne#')

Again i am not sure if I am making sense to anyone.
Who is Participating?
Yes, you can do this so long as you are not using CFGrid to edit the data.  If all the user needs to be able to do is select a cell, column, or row, this works fine.

The key change from your code is that you don't need selectedItem, and you have to set the SelectMode.

so lets say you want the click to select a row, and then reference column1 in that row, your code would look like this:
<cfform action="anotherpage.cfm">
     <cfgrid name="Grid" query="GridQuery" SelectMode="row">
          <cfgridcolumn name="ColumnOne" header="Column One">
          <cfgridcolumn name="ColumnTwo" header="Column Two">
          <!--- ... etc.  --->

and in anotherpage.cfm ->
<cfquery datasource="Table">
     INSERT INTO tableOne (GridItemNumber)
     VALUES ('#Form.Grid.ColumnOne#')
this will insert the value of ColumnOne in the query GridQuery (in the first .cfm file) for the row selected by the user on the cfgrid "Grid" into the column called GridItemNumber in the table tableOne

Which is what you want to do, right?


Here's the relevent section from the help file:

If the SelectMode attribute equals Single, Column, or Row, the data returned by form variables to the cfform's ACTION page contains information about which cell or cells were selected by the user. In general, this data is made available by ColdFusion in the ACTION page as ColdFusion variables in the form scope using the following naming convention:

Specifically, each SelectMode returns the following form variable(s):

form.#GridName#.#ColumnName# = "SelectedCellValue"

form.#GridName#.#ColumnName# = "ValueOfCellRow1, ValueOfCellRow2, ... ValueOfCellRowN"

form.#GridName#.#Column1Name# ="ValueOfCellInSelectedRow"

form.#GridName#.#Column2Name# = "ValueOfCellInSelectedRow"

form.#GridName#.#ColumnNName# = "ValueOfCellInSelectedRow"

myeritAuthor Commented:
You are a genius!

Thanks much, that is exactly what I needed!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.