Radcombobox in Radgrid-unable to set the current value of combobobox in edit mode

1 Answer 57 Views
Ajax
Neil
Top achievements
Rank 1
Neil asked on 07 Dec 2022, 09:00 PM | edited on 12 Dec 2022, 03:49 PM
Hi,

This one has been hard to find an answer for despite checking many posts. I have a radgrid with an embedded radcombobox. The fields in the grid look like this with a GridTemplateColumn that contains the radcombobox called cboGuageName:
<telerik:GridBoundColumn UniqueName="MeasureName" DataField="MeasureName" HeaderText="MeasureName" ReadOnly="true"></telerik:GridBoundColumn>
<telerik:GridBoundColumn UniqueName="Min" DataField="Min" HeaderText="Min"></telerik:GridBoundColumn>
<telerik:GridBoundColumn UniqueName="Max" DataField="Max" HeaderText="Max"></telerik:GridBoundColumn>
<%--<telerik:GridBoundColumn UniqueName="GuageName" DataField="GuageName" HeaderText="GuageName"></telerik:GridBoundColumn>--%>
<telerik:GridTemplateColumn UniqueName="GuageName" HeaderText="Guage Name" DataField="GuageName" HeaderStyle-Width="200px">
    <itemtemplate>
        <%# Eval("GuageName")%>
    </itemtemplate>
    <edititemtemplate>
        <telerik:RadComboBox ID="cboGuageName" OnLoad="cboGuageName_Load" AutoPostBack="true" DataTextField="GuageName" DataValueField="GuageName" SelectedValue='<%# Bind("GuageName") %>' runat="server" Width="140px"></telerik:RadComboBox>
    </edititemtemplate>
</telerik:GridTemplateColumn>


 I am able to set the radcombobox in the grid with the correct value and display the grid when not in edit mode using the following code in needdata source code event as follow:
if (lstDocType.SelectedIndex >= 0)
{
    using (var db = new SpecCheck.Models.TorqDevEntitiesNewTest())
    {
        string dbase = db.Database.Connection.ConnectionString;
        RadWindowManager1.RadAlert("Database is " + dbase + "-Need", 400, 180, "ALERT", null, null);

        Guid docId = new Guid(lstDocType.SelectedItem.Value.ToString());
        var measure = db.DocumentMeasureTypes.Where(d => d.DocumentTypeId == docId).Select(d => new { d.DocumentMeasureTypeId, d.MeasureName, d.Min, d.Max, d.GuageName, d.Sort }).OrderBy(d => d.Sort).ToList();
        RadGridMeasureType.DataSource = measure;
        //RadDropDownList ddl1 = edit1["Description"].FindControl("cboDocType") as RadDropDownList;
    }
}


When  click the edit link the code below executes which is in the itemdata event for the radgrid; it finds the right key, retrieves the correct grid data for the row, and sets the radcombobox selected with the value for that field. .However the code that grabs the combobox returns null so there is nothing to assign the gridrow's value to. Worst of all the program will not go  up into editable mode after clicking the edit link; it either does nothing or the cursor spins endlessly.
Here is the code in itemdata event for the grid:
described above: 
using (var dbMeasureType = new SpecCheck.Models.TorqDevEntitiesNewTest())
    //if (e.Item is GridDataItem)
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem editItem = (GridEditableItem)e.Item;
        //Access the RadDropDownList
        RadComboBox guageType = (RadComboBox)editItem.FindControl("GuageName");
        var key = (Guid)editItem.GetDataKeyValue("DocumentMeasureTypeId");
        //using (var db = new TorqDevEntitiesNewTest())
        //{
        var measures = dbMeasureType.DocumentMeasureTypes.Find(key);
        guageType.SelectedValue = (String)(measures.GuageName);
    }



 I've tried many things but nothing seems to work. Any advice would be appreciated.

Neil

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 12 Dec 2022, 04:17 PM

Hi Neil,

The problem is with the ID used to find the Combo

The ComboBox in the Column has the ID "cboGuageName".

<telerik:RadComboBox ID="cboGuageName" runat="server">
</telerik:RadComboBox>

 

Yet the code is trying to access the Combo by another ID "GaugaeName"

RadComboBox guageType = (RadComboBox)editItem.FindControl("GuageName");

Please use Combo's Correct ID for the FindControl() method.

Since the exception did not show up for you to notice, I am also assuming that AJAX is enabled. In that case, the server exception will show as a JavaScript error. The app will run, but will not behave anymore. You can inspect that in the Browser's DevTools and find a long exception coming from the ScriptResources.

I highly recommend disabling AJAX when testing/debugging the application.

Here are a few Blog posts that share tips and tricks for debugging the application:

 

Furthermore, I have noticed that the Combo's SelectedValue was bound to the Cell using the Bind() expression.

<telerik:RadComboBox ID="cboGuageName" runat="server" SelectedValue='<%# Bind("GuageName") %>'>
</telerik:RadComboBox>

 

If you do that, you should not change that in the ItemDataBound event (assuming this is the event that is called when you click on Edit)

protected void RadGridMeasureType_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item.IsInEditMode)
    {
        GridEditableItem editItem = (GridEditableItem)e.Item;

        RadComboBox guageType = (RadComboBox)editItem.FindControl("GuageName");

        guageType.SelectedValue = "Somevalue";
    }
}

The code does not throw exceptions, but the Bind() expression will lose its purpose if you change the SelectedValue manually.

 

I hope this will help resolve the issue.

 

Regards,
Attila Antal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Ajax
Asked by
Neil
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Share this question
or