This is a migrated thread and some comments may be shown as answers.

RadGrid DropDownColumn not rebinding to dynamically added datasource on edit

1 Answer 54 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jim
Top achievements
Rank 1
Jim asked on 14 Aug 2013, 12:56 PM
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.

1 Answer, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 19 Aug 2013, 08:40 AM
Hello Jim,

Can you please verify that you are not using DataBind()? Performing complex grid operations such as Inserting, Deleting, Updating, Hierarchy relations, Grouping, Paging, Sorting, Filtering, etc. require accommodating appropriate database operations.  Therefore, we suggest you to avoid Simple Databinding and strongly recommend the use of more advanced databinding methods, which automatically handle the aforementioned functions:
Declarative DataSource
Advanced Data Binding

Furthermore, please confirm that you are closely following the steps provided in the following article:
http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html

In case you want to change the grid structure dynamically, this topic explains how to do that:
http://www.telerik.com/help/aspnet-ajax/grid-changing-structure-dynamically.html

Hope this helps.

Regards,
Eyup
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Jim
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Share this question
or