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

Setting or Binding From Column Item Template Text/Value To Selected Value In Custom Edit Temple

0 Answers 437 Views
Grid
This is a migrated thread and some comments may be shown as answers.
jgill
Top achievements
Rank 1
jgill asked on 07 Jun 2012, 04:19 AM
UPDATE: Decided just to use session variables to track the state and that works.

Hello,
For the past two days I have been having a difficult time with a particular issue related to selecting or binding the selected value in a RadComboBox within a custom grid edit template column that I cannot find examples for exactly and have not been able to get the scenario to work.

Scenario
There is a 100% programmatically created RadGrid where the columns are dynamically created.  There is a GridTemplateColumn that can be created with a custom item template and edit template.  The item template is relatively simple with just a literal control in it, while the edit template has a RadComboBox.  The reason a GridDropDownColumn is not utilized is that the items in the RadComboBox contain localized text based on the user's context/language and this cannot be determined through a DataSourceID. When the user edits a row they see the correct items populated in the RadComboBox for that particular template and column, and the item template reflects their change (e.g. if they selected "Female" from the RadComboBox and save/insert their change they see the text "Female" in the item/non-edit tample).  However when they edit a row after inserting the value the RadComboBox does not select the value they previously entered as the selected value. 

Issue
The issue is that I cannot seem to find a way to bind the RadComboBox within the EditTemplate's selected value to the matching value from the text/item template.  Every time the user clicks edit (the edit mode is PopUp) all the regular column values get set to the same values they were in read/non-edit mode, except the RadComboBox in the custom edit column template.

Maybe in the RadGrid's ItemDataBound event there is a way to get the value from the the label control in the item template and use that to set (or try to find then set) the selected value in the RadComboBox in the edit template?  Or maybe i'm missing something crucial to the process.


Links Previously Viewed:
http://www.telerik.com/community/forums/aspnet/grid/radgrid-bind-dropdownlist-after-edit-mode.aspx (I cannot use the DataRowView type code without an exception like 'Unable to cast object of type 'Telerik.Web.UI.GridInsertionObject' to type 'System.Data.DataRowView'.

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)    
    {    
        if ((e.Item is GridEditableItem || e.Item is GridDataItem) && e.Item.IsInEditMode)    
        {    
            GridEditableItem editForm = (GridEditableItem)e.Item;    
            (editForm.FindControl("ddlGridAdjBy") as DropDownList).SelectedValue = ((System.Data.DataRowView)(editForm.DataItem)).Row.ItemArray[1].ToString();     // throws an exception here
        }    
    }

Many other links and of course http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html

Code
Please note I utilized the converter at http://converter.telerik.com to convert VB.NET to C# since most people are working in C#.  The converter sometimes does not convert things exactly.




protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode) {
        GridEditFormItem editItem = (GridEditFormItem)e.Item;
        RadComboBox dropdownUOM = (RadComboBox)editItem.FindControl("editList");
 
        // This PopulateDropDown works in populating the control with items
        //
        //For Each item As Something In CollectionThatGetsIterated
        //    Dim NewListItem As New RadComboBoxItem()
        //    NewListItem.Text = GetLocalizedText(item.Something1, item.Something2)
        //    NewListItem.Value = GetValue(item.Value)
 
        //    theDropDownControl.Items.Add(NewListItem)
        //Next
        PopulateDropDown(21, dropdownUOM);
        dropdownUOM.DataBind();
 
    }
 
}

This is the setup specific to this type of column, assume the column unique name, etc. have already been set.
// This is what sets the item and edit templates.  Since the items in the dropdown are added to the Items collection in a loop vs. against a dataset/collection the ListTextField and ListValueFields are not set.  
private void BuildLookupListColumn(string columnName, GridTemplateColumn existingTemplateColumn)
{
    existingTemplateColumn.ItemTemplate = new CustomItemTemplate(columnName);
 
    KeyValuePair<string, string> emptyListItem = new KeyValuePair<string, string>("Select", string.Empty);
    existingTemplateColumn.EditItemTemplate = new CustomEditTemplate(columnName, emptyListItem);
    existingTemplateColumn.ConvertEmptyStringToNull = true;
}

Edit Template: 
public class CustomEditTemplate: ITemplate, IBindableTemplate
{
 
    private string _columnName;
 
    private KeyValuePair<string, string> _emptyListItem;
 
    public CustomEditTemplate()
    {
    }
 
    public CustomEditTemplate(string columnName, KeyValuePair<string, string> emptyListItem)
    {
        this.ColumnName = columnName;
    }
 
    public string ColumnName {
        get { return _columnName; }
        set { _columnName = value; }
    }
 
    public KeyValuePair<string, string> EmptyListItem {
        get { return _emptyListItem; }
        set { _emptyListItem = value; }
    }
 
    public void InstantiateIn(Control container)
    {
        RadComboBox displayControl = new RadComboBox();
        displayControl.ID = "editList";
 
        displayControl.DataBinding += DisplayControl_DataBinding;
        container.Controls.Add(displayControl);
    }
 
    private void DisplayControl_DataBinding(object sender, EventArgs e)
    {
        //Dim target As RadComboBox = DirectCast(sender, RadComboBox)
        //Dim container As GridEditFormItem = DirectCast(target.NamingContainer, GridEditFormItem)
 
        //Dim displayControl As RadComboBox = DirectCast(sender, RadComboBox)
        //'Note: This line below throws a binding error on insert DataBinding: 'Telerik.Web.UI.GridInsertionObject' does not contain a property with the name 'AccountSettings'.
        //displayControl.SelectedValue = DataBinder.Eval(container.DataItem, ColumnName).ToString()
 
        //displayControl.SelectedValue = displayControl.SelectedValue
    }
 
    public System.Collections.Specialized.IOrderedDictionary ExtractValues(Control container)
    {
        OrderedDictionary dict = new OrderedDictionary();
        string value;
 
        value = (((RadComboBox)((GridEditFormItem)container).FindControl("editList")).SelectedValue);
 
        dict.Add(ColumnName, value);
        return dict;
    }
}

Item Template: 
public class CustomItemTemplate : ITemplate
{
 
 
    private string _columnName;
 
    public CustomItemTemplate()
    {
    }
 
    public CustomItemTemplate(string columnName)
    {
        this.ColumnName = columnName;
    }
 
    public string ColumnName {
        get { return _columnName; }
        set { _columnName = value; }
    }
 
    public void InstantiateIn(Control container)
    {
        LiteralControl displayControl = new LiteralControl();
        displayControl.ID = "litEditList";
        displayControl.DataBinding += DisplayControl_DataBinding;
        container.Controls.Add(displayControl);
    }
 
    private void DisplayControl_DataBinding(object sender, EventArgs e)
    {
        LiteralControl target = (LiteralControl)sender;
        GridDataItem container = (GridDataItem)target.NamingContainer;
        target.Text = (DataRowView)container.DataItem(_columnName).ToString();
    }
}


I want to do this without using Session variables.  It seems really "hacky" to utilize them to store the drop down/combobox's selected state.  Also imagine the case, where there are 20 rows of data in the RadGrid with multiple columns in each row that could be of this EditTemplate type with the dropdons, there are quite a few session variables that would be around just to track the selected state and they'd stick around until session expiration unless they are explicitly removed.

No answers yet. Maybe you can help?

Tags
Grid
Asked by
jgill
Top achievements
Rank 1
Share this question
or