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

Possible binding problems within a RadGrid FormTemplate

2 Answers 60 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Boris
Top achievements
Rank 1
Boris asked on 08 Nov 2012, 04:42 PM
In a previous forum thread ( http://www.telerik.com/community/forums/aspnet-ajax/grid/is-this-conventional-practice-with-a-radgrid.aspx ) 'Marin' suggested that I could move the RadComboBoxes to the ItemDataBound event.

First of all, keep in mind that the RadComboBoxes have conventional Bind statements on their SelectedValue property and that they are in a RadGrid FormTemplate.

When I tried to move their population calls to ItemDataBound (within a 'if (e.Item is GridEditFormItem && e.Item.IsInEditMode)' block) it crashed.

So I suppose my question has two parts.

First: Is there a demo that does this?

Second: How do I prevent a RadComboBox from crashing on a bad incoming value, i.e. one that isn't in it's list? 
(This is more of a deja vu thing.  I'll swear I've done this before but I can't find the code...)

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 09 Nov 2012, 04:29 AM
Hi,

You can bind the selected value in combobox as shown below.
C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
 if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
 {
   GridEditFormItem editItem = (GridEditFormItem)e.Item;
   RadComboBox combo = (RadComboBox)editItem.FindControl("RadComboBox1");
   combo.SelectedValue = (string)DataBinder.Eval(e.Item.DataItem, "Id").ToString();
 }
}
Also check the following demo which implements similar scenario.
Grid / Combo in Grid

Thanks,
Shinu.
0
Boris
Top achievements
Rank 1
answered on 13 Nov 2012, 06:27 PM
I'm afraid I'm having all kinds of problems. The correct data is getting into the DataSource but it crashes when I try to do a DataBind.  
'System.NotSupportedException.'   It's complaining that {"Object of type Object1 does not have a FullName property."} (which it most definitely does).   And in spite of correct data, it's displaying an empty list.

What I've done is essentially as follows.  I needed to create a RadCombboBox with a 'blank' value.  I first removed the Bind statement from the FormTemplate and the original data source code in RadGrid1_ItemCreated.  

Then I defined a struct:  
 public struct blankObject
    {
        public string FullName;
        public int Id;
        public blankObject(string sFullName, int iId)
        {
            this.FullName=sFullName;
            this.Id = iId;
        }
    }

I then created a method in my data layer, in a static class.  (This was the product of some trial and error, from sources around the net.)
 public static IQueryable<blankObject> getAssignedTo2(DataContext1 DbContext)
 {
       var x = new List<blankObject> { new blankObject("Not Selected", 0) };
       var p = from u in DbContext.Table1                    
                    select new blankObject { FullName = u.Name, Id= u.Id };
       return x.AsQueryable().Union(p);
}

Finally I call this code in the RadGrid1_ItemDataBound class.
ddl1.DataSource = StaticClass.getList(DbContext);
 ddl1.DataTextField = "FullName";
 ddl1.DataValueField = "Id";                       
 ddl1.SelectedValue = (string)DataBinder.Eval(e.Item.DataItem, "AnotherId").ToString();
 ddl1.DataBind();

And it always crashes on the DataBind.

Tags
Grid
Asked by
Boris
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Boris
Top achievements
Rank 1
Share this question
or