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

Hierarchical grid update problem

1 Answer 24 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Larry
Top achievements
Rank 1
Larry asked on 22 Sep 2014, 06:24 PM

I'm having a little difficulty with updating a hierarchical grid.  My gird has 3 levels, orders,deliveries and schedules.  On my top level, I want to be able to directly update the grid row on a few of the columns.  I have a sample program where I do the same thing on a normal grid and it works.  Identical code in my Hierarchical grid does not.  The problem I'm having is when it come time for me to assign the grid item it's new value.  For some reason, my grid item is being determined as read-only upon assignment and is failing.  I've confirmed that the item is not read-only, but when I try to update it, it's being rendered as read-only and I don't know why.  My update routine is as follows.  Any help with this would be appreciated.  Thanks.

 protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
       
        foreach (GridColumn column in e.Item.OwnerTableView.RenderColumns)
        {
            if (column is IGridEditableColumn)
            {
                IGridEditableColumn editableCol = (column as IGridEditableColumn);
                if (editableCol.IsEditable)
                {
                    GridEditableItem editedItem = e.Item as GridEditableItem;
                    object oldval = editedItem.SavedOldValues[column.UniqueName];
                    GridEditManager editMan = editedItem.EditManager;
                    IGridColumnEditor editor = editMan.GetColumnEditor(editableCol);
                    object editorValue = null;
                    editorValue = (editor as GridTextColumnEditor).Text;
                    try
                    {
                    
                       DataRow[] changedRows = this.ItemSource.Select("ORDER = '" + editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]        ["ORDER"].ToString() + "'");
                       changedRows[0][column.UniqueName] = editorValue;           // This is the line where the error is occurring !!!
                       ItemSource.AcceptChanges();
                      
                    }  
                    catch (Exception ex)
                    {
                        Label1.Text = "<strong>Unable to set value of column '" + column.UniqueName + "'</strong> - " + ex.Message;
                        e.Canceled = true;
                        break;
                    }
                }
            }
        }

    }

1 Answer, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 25 Sep 2014, 10:30 AM
Hello Larry,

You do not need to iterate through the columns in the RadGrid and perform the same logic for every column. In order to update the values you could use a bit different logic. Check if the current item is GridEditableItem and then update the values for every cell in the row. The code would look similar to the following:

protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
    var editItem = e.Item as GridEditableItem;
 
    if (editItem is GridEditableItem && editItem.IsInEditMode)
    {
        DataTable newData = GridData;
 
        DataRow[] row = newData.Select("ID = " + editItem.OwnerTableView.DataKeyValues[editItem.ItemIndex]["ID"].ToString());
 
        foreach (var column in editItem.OwnerTableView.RenderColumns)
        {
            if (column is GridEditableColumn)
            {
                row[0][column.UniqueName] = (editItem[column.UniqueName].Controls[0] as TextBox).Text;
            }
        }
 
        newData.AcceptChanges();
    }
}



Regards,
Viktor Tachev
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.

 
Tags
Grid
Asked by
Larry
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Share this question
or