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

[Solved] GetColumnEditor ArgumentOutOfRangetException

2 Answers 181 Views
Grid
This is a migrated thread and some comments may be shown as answers.
eakins05
Top achievements
Rank 1
eakins05 asked on 24 Jun 2009, 03:48 PM
I have an editable grid that needs to have one of its cells conditionally editable.  I decided that the best place to make this check would probably be the EditCommand event.  My rationale was that when an item was clicked to be edited, I would grab the ID from it, and if it was greater than 0, I would grab the GridTextBoxColumnEditor for the DataKey cell and set its TextBox to Enabled = false.  Below is my code snippet.
    <telerik:RadGrid ID="coreDataEntriesGrid" runat="server" Width="700" EnableViewState="true" HorizontalAlign="Center" 
     AutoGenerateColumns="false" AllowSorting="false" OnRowDrop="CoreDataEntriesGrid_RowDrop" Skin="Web20" 
     OnNeedDataSource="CoreDataEntriesGrid_NeedDataSource" OnUpdateCommand="CoreDataEntriesGrid_UpdateCommand" 
     OnInsertCommand="CoreDataEntriesGrid_InsertCommand" OnEditCommand="CoreDataEntriesGrid_EditCommand"
        <MasterTableView DataKeyNames="ObjectID,DataKey,DataValue,IsActive" EditMode="InPlace" CommandItemDisplay="Top"
            <Columns> 
                <telerik:GridEditCommandColumn ButtonType="LinkButton" CancelText="Cancel" EditText="Edit" UpdateText="Update"  /> 
                <telerik:GridBoundColumn HeaderText="Key" DataField="DataKey" ItemStyle-Width="100" 
                 ColumnEditorID="dataKeyEditor" UniqueName="DataKey" /> 
                <telerik:GridBoundColumn HeaderText="Value" DataField="DataValue" ItemStyle-Width="500" 
                 ColumnEditorID="dataValueEditor" UniqueName="DataValue" /> 
                <telerik:GridCheckBoxColumn HeaderText="Active" DataField="IsActive" ItemStyle-Width="50"  
                 UniqueName="IsActive" /> 
            </Columns> 
        </MasterTableView> 
        <ClientSettings AllowRowsDragDrop="true"
            <Selecting AllowRowSelect="true" /> 
            <ClientEvents OnRowDropping="CoreDataEntriesGrid_RowDropping" /> 
        </ClientSettings> 
    </telerik:RadGrid> 
     
    <telerik:GridTextBoxColumnEditor ID="dataKeyEditor" runat="server" TextBoxStyle-Width="100" /> 
    <telerik:GridTextBoxColumnEditor ID="dataValueEditor" runat="server" TextBoxStyle-Width="500" /> 

        protected void CoreDataEntriesGrid_EditCommand(object source, GridCommandEventArgs e) 
        { 
            GridEditableItem item = e.Item as GridEditableItem; 
            int id = (int) item.GetDataKeyValue("ObjectID"); 
            if (id > 0) 
            { 
                (item.EditManager.GetColumnEditor("DataKey"as GridTextBoxColumnEditor).TextBoxControl.Enabled = false
            } 
        } 
 

What's happening is that I'm getting an ArgumentOutOfRangeException on the line item.EditManager.GetColumnEditor("DataKey").  I figured ok, this is obviously not the right event to tap into.  However, when I was in debug mode, I put a watch on item.EditManager.GetColumnEditor("IsActive") and that returned the correct GridCheckboxColumnEditor without tossing the exception.  The only thing that I could tell that was different was the fact that I'm setting the ColumnEditorID for the DataKey column and not on the IsActive column (besides the obvious fact that they're different column types), so I tried taking it out and letting the BoundColumn handle it on its own, but that had the same result.  Could you point me in the right direction?

Thanks,
Jared

2 Answers, 1 is accepted

Sort by
0
Accepted
Rosen
Telerik team
answered on 29 Jun 2009, 01:27 PM
Hi,

The reason for CheckBoxColumn not throwing exception is that the edit control do exists in both edit and view modes as opposite to TextBoxEditor where the TextBox control is only available in edit mode.

In order to achieve the desired functionality I suggest to use ItemCreated event instead, similar to the following:

protected void coreDataEntriesGrid_ItemCreated(object sender, GridItemEventArgs e)  
    {  
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)  
        {  
            GridEditableItem item = e.Item as GridEditableItem;  
            int id = (int)item.GetDataKeyValue("ObjectID");  
            if (id > 0)  
            {  
                (item.EditManager.GetColumnEditor("DataKey"as GridTextBoxColumnEditor).TextBoxControl.Enabled = false;  
            }  
        }  
    } 


Regards,
Rosen
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
eakins05
Top achievements
Rank 1
answered on 29 Jun 2009, 01:57 PM
Thanks a lot, that works great.  One small thing however: It blows up on the GetDataKeyValue call when doing an insert on the grid (because e.Item is GridEditableItem and item.IsInEditMode is true).  That was remedied by checking that the DataItem of the GridEditableItem was the type being bound to it (it was of type GridInsertionItem in the opposite case).  Thanks again!
Tags
Grid
Asked by
eakins05
Top achievements
Rank 1
Answers by
Rosen
Telerik team
eakins05
Top achievements
Rank 1
Share this question
or