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

ArgumentOutOfRangeException in RadGrid_ItemDataBound

3 Answers 238 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Peter
Top achievements
Rank 1
Peter asked on 25 Nov 2014, 05:36 PM
I'm getting an exception System.ArgumentOutOfRangeException thrown in the ItemDataBound callback of a RadComboBox that is in a GridDropDownColumn; breaking on editableItem.GetDataKeyValue() after inserting an item.  Can someone explain what is going on and what I'm need to do to correct it?

I have a grid that is bound via OnNeedDataSource to a table. A second table is bound (in code) to a column, though disabling it still shows the problem.  Ultimately I'm trying to select the customer in the combo box to the current item, but if there isn't a customer then to select the first "No Customer" item.

<telerik:RadGrid runat="server" ID="Test_RadGrid" AutoGenerateColumns="false" AllowPaging="true"
    OnNeedDataSource="RadGrid_NeedDataSource" OnUpdateCommand="RadGrid_UpdateCommand"
    OnItemCreated="RadGrid_ItemCreated" OnDeleteCommand="RadGrid_DeleteCommand"
    OnInsertCommand="RadGrid_InsertCommand" OnItemDataBound="RadGrid_ItemDataBound">
    <MasterTableView DataKeyNames="ID" CommandItemDisplay="Top" InsertItemPageIndexAction="ShowItemOnCurrentPage" EditMode="InPlace">
        <Columns>
            <telerik:GridEditCommandColumn ButtonType="ImageButton" />
            <telerik:GridBoundColumn DataField="ID" HeaderText="ID" ReadOnly="true" ForceExtractValue="Always" ConvertEmptyStringToNull="true" />
            <telerik:GridBoundColumn DataField="Name" HeaderText="Name" />
            <telerik:GridDropDownColumn UniqueName="CustomerCombo" HeaderText="Customer" DropDownControlType="RadComboBox" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

protected void RadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
    Test_RadGrid.DataSource = DataSource(); // linq to sql join statement
}

protected void RadGrid_ItemDataBound(object source, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem editableItem = e.Item as GridEditableItem;
 
        // ^---- At this point editableItem.KeyValues, in Watch window, says KeyValues threw an exception
        // of type System.ArgumentOutOfRangeException, but doesn't break
 
        GridEditManager editManager = editableItem.EditManager as GridEditManager;
        GridDropDownListColumnEditor editor = editManager.GetColumnEditor("CustomerCombo") as GridDropDownListColumnEditor;
 
        //editor.DataSource = Customer.DataSource();
        //editor.DataValueField = TestField.ID;
        //editor.DataTextField = TestField.Name;
        //editor.DataBind();
 
        RadComboBox combo = editableItem[TestField.CustomerCombo].Controls[0] as RadComboBox;
        combo.AppendDataBoundItems = true;
        combo.Items.Insert(0, new RadComboBoxItem("No customer selected", "0"));
 
        var ret = (int)editableItem.GetDataKeyValue("0"); //  <-- OutOfRangeException thown, debugger
    }



3 Answers, 1 is accepted

Sort by
0
Peter
Top achievements
Rank 1
answered on 25 Nov 2014, 06:22 PM
To add a bit more context...this is when adding a record
0
Accepted
Konstantin Dikov
Telerik team
answered on 28 Nov 2014, 08:42 AM
Hello Peter,

The error that you are receiving is rather expected, because you can not retrieve DataKeyValue from insert item. To handle this within the OnItemDataBound event you must ensure that the item is not an insert item, before calling the GetDataKeyValue method:
if (!(editableItem is GridDataInsertItem))
{
    var ret = (int)editableItem.GetDataKeyValue("ID");
}

Hope this helps.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Peter
Top achievements
Rank 1
answered on 02 Dec 2014, 10:06 PM
It cleared up how things work and got me onto the answer - I query the DataSource for the CustomerID and then combo.SelectedValue = customerID.  Thanks for the direction!





Tags
Grid
Asked by
Peter
Top achievements
Rank 1
Answers by
Peter
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or