Problem about multi-row edit

3 posts, 0 answers
  1. Liu Peng
    Liu Peng avatar
    18 posts
    Member since:
    Oct 2006

    Posted 02 Dec 2009 Link to this post

    I want use grid's multi-row edit, but I find a problem:
    1. Set three rows enter in-line edit mode.
    2. Change all these three rows values.
    3. Save the edit row #1
    4. After the edit row #1 saved, the new modified values for the other two edit row #2, #3 are lost.

    How to solve this problem?
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 02 Dec 2009 Link to this post

    Hi Liu,

    The described behavior is actually expected. If the user changes the values in edit mode and tries to update another row, then the grid will rebind itself and all modified data on client side will be lost. So one suggestion would be allowing the users edit and update only selected rows and hid the update/cancel buttons for each row as described in the following forum.
    Radgrid with AllowMultiRowEdit loses values

    Here is the online demo which implements this approach: Command Item

    -Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Liu Peng
    Liu Peng avatar
    18 posts
    Member since:
    Oct 2006

    Posted 04 Dec 2009 Link to this post

    Hi Shinu,

    Thanks for you reply, I just find a useful subject Multi-add and Multi-edit for Grid
    My solution is:
    1. Save all the column editor control values for each editable items at grid's ItemCommand event.
    void _radGrid_ItemCommand(object source, GridCommandEventArgs e) 
        if (e.CommandName == RadGrid.EditCommandName || 
            e.CommandName == RadGrid.UpdateCommandName || 
            e.CommandName == RadGrid.CancelCommandName) 
        { 
            SaveColumnEditorCurrentValues(); 
        } 
     
    // Save the current values for the column editor controls 
    // because of the column editor control's new input value 
    // will be lost when invoke Edit,Update,Cancel.  
    private void SaveColumnEditorCurrentValues() 
        this._currentEditRowsValues = new Dictionary<int, Dictionary<intobject>>(); 
     
        foreach (GridDataItem item in this.MasterTableView.Items) 
        { 
            if (item.IsInEditMode) 
            { 
                int key = (int)item.GetDataKeyValue("fldID"); 
                Dictionary<intobject> row = new Dictionary<intobject>(); 
     
                // then only retrive these editable fields values from the editor control 
                // update some values base on the inline editor control 
                this.DBSettings.CustomizedListColumns.ForEach(delegate(GridCustomizedListColumn column) 
                { 
                    if (column.IsEditable) 
                    { 
                        object newValue = null
                        IGridColumnEditor editor = item.EditManager.GetColumnEditor(column); 
     
                        if (editor is GridCustomizedListColumnCheckBoxEditor) 
                        { 
                            newValue = ((GridCustomizedListColumnCheckBoxEditor)editor).CheckBoxControl.Checked; 
                        } 
                        else if (editor is GridCustomizedListColumnComboBoxEditor) 
                        { 
                            RadComboBox comboBox = ((GridCustomizedListColumnComboBoxEditor)editor).ComboBoxControl; 
                            // only save valid item 
                            if (!string.IsNullOrEmpty(comboBox.SelectedValue)) 
                                newValue = new RadComboBoxItem(comboBox.Text, comboBox.SelectedValue); 
                        } 
                        else if (editor is GridCustomizedListColumnDateTimeEditor) 
                        { 
                            newValue = ((GridCustomizedListColumnDateTimeEditor)editor).PickerControl.SelectedDate; 
                        } 
                        else if (editor is GridCustomizedListColumnTextBoxEditor) 
                        { 
                            newValue = ((GridCustomizedListColumnTextBoxEditor)editor).TextBoxControl.Text; 
                        } 
                        else if (editor is GridTextColumnEditor) 
                        { 
                            newValue = ((GridTextColumnEditor)editor).Text; 
                        } 
                        else 
                        { 
                            newValue = null
                        } 
     
                        row.Add(column.ID, newValue); 
                    } 
                }); 
     
                this._currentEditRowsValues.Add(key, row); 
            } 
        } 

    2. Re-set these saved value to the column editor control.
    I create a new class GridCustomizedListColumn inherit from GridBoundColumn, the following codes referenced Telerik's source code.
    public class GridCustomizedListColumn : GridBoundColumn 
     
        public override void InitializeCell(TableCell cell, int columnIndex, GridItem inItem) 
        { 
            base.InitializeCell(cell, columnIndex, inItem); 
            if (inItem.IsDataBound && 
                this.FieldIsInlineEditable) 
            { 
                inItem.CellDataBound += new GridCellDataBoundEvent(OnDataBindColumn); 
            } 
        } 
     
        private void OnDataBindColumn(object sender, GridCellDataBoundEventArgs args) 
        { 
            if (args.Column != this
            { 
                return
            } 
     
            GridDataItem item = (GridDataItem)sender; 
            if (!item.IsDataBound) 
            { 
                return
            } 
     
            object dataItem = item.DataItem; 
            if (dataItem == null
            { 
                return
            } 
     
            if (this.DataField == null || this.DataField.Length == 0) 
            { 
                return
            } 
     
            if (base.DesignMode) 
            { 
                return
            } 
     
            if (args.Cell == null
            { 
                return
            } 
     
            if (item.IsInEditMode /*&& !this.ReadOnly */
            { 
                this.CurrentColumnEditor.InitializeFromControl(args.Cell); 
                if (!this.CurrentColumnEditor.IsInitialized) 
                { 
                    return
                } 
            } 
     
            object val = null
     
            try 
            { 
                // try to find the current value 
                int key = (int)item.GetDataKeyValue("fldID"); 
                if (this.OwnerGrid.CurrentEditRowsValues.ContainsKey(key)) 
                { 
                    val = this.OwnerGrid.CurrentEditRowsValues[key][this.ID]; 
                } 
     
                if (val != null && val.ToString() != string.Empty) 
                { 
                    IGridColumnEditor editor = this.CurrentColumnEditor; 
                    if (editor is GridCustomizedListColumnComboBoxEditor) 
                    { 
                        RadComboBox comboBox = ((GridCustomizedListColumnComboBoxEditor)editor).ComboBoxControl; 
                        RadComboBoxItem comboBoxitem = val as RadComboBoxItem; 
                        if (comboBox.EnableLoadOnDemand && comboBoxitem != null
                        { 
                            comboBox.Items.Clear(); 
                            comboBoxitem.Selected = true
                            comboBox.Items.Add(comboBoxitem); 
                        } 
                    } 
                    else if (editor is GridTextColumnEditor) 
                    { 
                        ((GridTextColumnEditor)editor).Text = val.ToString(); 
                    } 
                } 
            } 
            catch 
            { 
                // 
            } 
        } 


Back to Top