I'm dynamically building a grid's columns on page load based on a metadata structure I have in the database. The grid is building properly including a variety of column types. For the DropDown column, I'm dynamically adding an additional datasource to the form and setting it's select command as well.
protected
void
Page_Load(
object
sender, EventArgs e)
{
var tableName = Request.QueryString[
"table"
];
if
(tableName !=
null
)
{
if
(!Page.IsPostBack)
{
{
LayoutGrid(tableName);
}
}
BindData(tableName);
}
}
private
static
GridColumn AppendDropDownColumn(MetaColumn columnInfo, RadGrid target)
{
var relatedColumn = columnInfo.LeftRelatedColumn.FirstOrDefault();
if
(relatedColumn ==
null
)
// Can't find relationship. Just bind as a text column.
return
AppendTextColumn(columnInfo, target);
var col =
new
GridDropDownColumn();
target.Columns.Add(col);
col.ListTextField = relatedColumn.RightDisplayColumn.DatabaseColumnName;
col.ListValueField = relatedColumn.RightValueColumn.DatabaseColumnName;
col.DataField = columnInfo.DatabaseColumnName;
col.HeaderText = columnInfo.Alias;
// Dynamically add datasource for the item list. RAD Controls don't allow setting the datasource directly
var itemsSource =
new
SqlDataSource();
itemsSource.ConnectionString = ConfigurationManager.ConnectionStrings[
"DynamicWebDb"
].ConnectionString;
itemsSource.SelectCommand = String.Format(
"Select {0}, {1} FROM {2} ORDER BY {0}"
,
col.ListTextField, col.ListValueField, relatedColumn.RightDisplayColumn.MetaEntity.EntityName);
itemsSource.ID =
"Lookup"
+ columnInfo.ColumnId;
target.Parent.Controls.Add(itemsSource);
col.DataSourceID = itemsSource.ID;
return
col;
}
This appears to be binding properly as the display value is being displayed rather than the code in view mode,
but if I shift to edit mode, the dropdown column looses it's data source values. What do I need to do differently
to re-display the dropdown list values correctly when editing. I would prefer not to have to reconfigure the
datasources on each postback.