I'm trying to develop an grid that is entirely dynamic - the columns are defined in the code-behind, as a result of a database query.
In insert/edit, some of the columns need to be displayed as dropdown lists - and for some of them the contents of the list are the result of a database query, and for others the content is a fixed set of strings.
Right now, I'm working with the latter, thinking it'd be simpler. Populating the dropdowns from the database I'll worry about after I get the fixed dropdowns working.
You have a note one one of your pages to the effect that the GridDropDownColumn isn't really intended for use with custom object collections, and that I should consider using a template column. My understanding is that template columns, when added in code-behind, need to be added in Page_Init() - which would seem to rule them out, as I won't know what columns need to be added that early in the page life-cycle. So GridDropDownColumn is what I have to work with.
In Page_Load, when !IsPostBack, I use the following code to create the column:
And then in OnItemDataBound, I populate the dropdown with:
When I display the grid, the foobar column is blank, though the "FOO" and "BAR" choices show up in the dropdown during edit.
I know the "foobar" DataField is correct, because when I use a GridBoundColumn instead of a GridDropDownColumn, the proper value displays in the grid. (Though, of course, in the edit form I get a textbox, instead of a dropdown.)
I'm clearly missing something. What?
In insert/edit, some of the columns need to be displayed as dropdown lists - and for some of them the contents of the list are the result of a database query, and for others the content is a fixed set of strings.
Right now, I'm working with the latter, thinking it'd be simpler. Populating the dropdowns from the database I'll worry about after I get the fixed dropdowns working.
You have a note one one of your pages to the effect that the GridDropDownColumn isn't really intended for use with custom object collections, and that I should consider using a template column. My understanding is that template columns, when added in code-behind, need to be added in Page_Init() - which would seem to rule them out, as I won't know what columns need to be added that early in the page life-cycle. So GridDropDownColumn is what I have to work with.
In Page_Load, when !IsPostBack, I use the following code to create the column:
GridDropDownColumn col = new GridDropDownColumn(); |
this.grdRecords.MasterTableView.Columns.Add(col); |
col.DataField = "foobar"; |
col.UniqueName = "foobar"; |
And then in OnItemDataBound, I populate the dropdown with:
if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode) |
{ |
GridEditableItem editedItem = e.Item as GridEditableItem; |
GridEditManager editMan = editedItem.EditManager; |
GridDropDownListColumnEditor editor = |
editMan.GetColumnEditor("foobar") as GridDropDownListColumnEditor; |
editor.DataSource = new object[] { "FOO", "BAR" }; |
editor.DataBind(); |
} |
When I display the grid, the foobar column is blank, though the "FOO" and "BAR" choices show up in the dropdown during edit.
I know the "foobar" DataField is correct, because when I use a GridBoundColumn instead of a GridDropDownColumn, the proper value displays in the grid. (Though, of course, in the edit form I get a textbox, instead of a dropdown.)
I'm clearly missing something. What?