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

Having problem in databinding to radcombo in GridEditableItem

3 Answers 216 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Vinodh Kumar
Top achievements
Rank 1
Vinodh Kumar asked on 09 Jul 2010, 04:51 PM
Hi,
i am using radgrid and edit template. In the edit template, we are using a dropdown control to select the data which will be based on certain conditions. These conditions are dynamic and hence need to use a object datasource or direct datatable binding to the dropdown. I am trying to bind the dropdown control in the itemdatabound event (please refer to the code below), but am getting an error (please refer to the error message below) whenever I try to execute the same. Need some solution for the same.

Thanks,

Vinodh
 
protected void radGridItemMetal_ItemDataBound(object sender, GridItemEventArgs e)
        {
            try
            {
                
                foreach (GridEditableItem editItem in radGridItemMetal.MasterTableView.GetItems(GridItemType.EditFormItem))
                {
                    if (editItem.IsInEditMode )
                    {
                        DataSet comboDataSet = items.GetComboDataset();
                        DataTable itemUOM = comboDataSet.Tables[0];
                        RadComboBox dropdownUOM = (RadComboBox)editItem.FindControl("radcomboUOM");
                        dropdownUOM.DataSource = itemUOM;
                        dropdownUOM.DataTextField = "LDValue";
                        dropdownUOM.DataValueField = "LDID";                        
                        dropdownUOM.DataBind();

                    }

                }
            }
            catch (Exception err)
            {

            }


The error it gives is::
{System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
   at System.Web.UI.Page.GetDataItem()
   at ASP.forms_items_aspx.__DataBinding__control167(Object sender, EventArgs e) in c:\Users\Sunil\AppData\Local\Temp\Temporary ASP.NET Files\root\7864bae7\722837af\App_Web_k0euui1y.1.cs:line 0
   at System.Web.UI.Control.OnDataBinding(EventArgs e)
   at Telerik.Web.UI.RadComboBox.OnDataBinding(EventArgs e)
   at Telerik.Web.UI.RadComboBox.PerformSelect()
   at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
   at Telerik.Web.UI.RadComboBox.DataBind()
   at MJJERP.PTL.Forms.Items.radGridItemMetal_ItemDataBound(Object sender, GridItemEventArgs e) in D:\Projects\MJJERP\Source\MJJ.PTL\Forms\Items.aspx.cs:line 120}

3 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 13 Jul 2010, 09:15 AM
Hi Vinodh,

First, you do not need to iterate over all your grid edited items, because ItemDataBound will be fired for each edited item anyway. You simply need to check for the current item:

protected void radGridItemMetal_ItemDataBound(object sender, GridItemEventArgs e)
{
    if(e.Item is GridEditFormItem && e.Item.IsInEditMode)
    {
        GridEditFormItem editItem = (GridEditFormItem)e.Item;
        DataSet comboDataSet = items.GetComboDataset();
        DataTable itemUOM = comboDataSet.Tables[0];
        RadComboBox dropdownUOM = (RadComboBox)editItem.FindControl("radcomboUOM");
        dropdownUOM.DataSource = itemUOM;
        dropdownUOM.DataTextField = "LDValue";
        dropdownUOM.DataValueField = "LDID";                       
        dropdownUOM.DataBind();
    }
}

Now, once the combo is databound, you need to programmatically set its SelectedValue after binding (in the above event handler). You cannot use Eval() or Bind() with controls that are databound manually. Therefore, if you have been using any of these for the above combo (or any other combo bound in the same way), this explains the exception. Remove Eval()s or BInd()s from the markup and set the selected item and value programmatically.

Regards,
Veli
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Vinodh Kumar
Top achievements
Rank 1
answered on 14 Jul 2010, 08:09 AM
Hi,
Thanks for the reply. We are using the dropdown control for the said scenario through out our project. So please provide some sample regarding the solution.
regards,
vinodh
0
Veli
Telerik team
answered on 14 Jul 2010, 09:40 AM
Hi Vinodh,

Attached is a sample test page demonstrating this approach.

Veli
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Vinodh Kumar
Top achievements
Rank 1
Answers by
Veli
Telerik team
Vinodh Kumar
Top achievements
Rank 1
Share this question
or