on enter key copy cell values from previous row

3 posts, 0 answers
  1. Andrea
    Andrea avatar
    9 posts
    Member since:
    Jan 2014

    Posted 14 Aug 2015 Link to this post

    Hi,
    to facilitate and speed up insert row, when user press enter key all blank cell in current row must be copied from same cell (same column) of previous row.

    At the begin the grid is blank, it not have datasource but only template for columns, user can add row by new row grid feature.

    I have some problem when the edit row is the new row (not jet added to rows grid collection) and cell is in edit mode.
    I try to create a custom GridBehavior to do this, but if row in edit is the new row i can't access it.

    Thank you very much.

     

  2. Andrea
    Andrea avatar
    9 posts
    Member since:
    Jan 2014

    Posted 15 Aug 2015 in reply to Andrea Link to this post

    <code> 

    private void radGridView1_KeyDown(object sender, KeyEventArgs e)
    {
    if (e.KeyCode == Keys.Enter)
    {
    GridViewRowInfo currRow = radGridView1.CurrentRow;
    GridViewRowInfo prevRow = null;
    if (radGridView1.GridNavigator.SelectPreviousRow(1))
    prevRow = radGridView1.CurrentRow;
    CopyCellFromPrevRow(prevRow, currRow);
    radGridView1.GridNavigator.SelectRow(currRow);
    radGridView1.GridNavigator.SelectNextRow(1);
    radGridView1.GridNavigator.SelectFirstColumn();
    }
    }

    private void radGridView1_UserAddedRow(object sender, GridViewRowEventArgs e)
    {
    if (editorEnter)
    {
    GridViewRowInfo from = null;
    if (radGridView1.GridNavigator.SelectRow(e.Row))
    {
    if (radGridView1.GridNavigator.SelectPreviousRow(1))
    from = radGridView1.CurrentRow;
    }
    CopyCellFromPrevRow(from, e.Row);
    radGridView1.GridNavigator.SelectRow(e.Row);
    radGridView1.GridNavigator.SelectNextRow(1);
    radGridView1.GridNavigator.SelectFirstColumn();
    }
    editorEnter = false;
    }
    private void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
    {
    //This Event Handler applies to columns of type GridViewTextBoxColumn
    RadTextBoxEditor activeEditor = e.ActiveEditor as RadTextBoxEditor;
    if (activeEditor != null)
    {
    RadEditorElement editor = activeEditor.EditorElement as RadEditorElement;
    if (editor != null)
    {
    editor.KeyDown += editor_KeyDown;
    }
    }

    }
    void editor_KeyDown(object sender, KeyEventArgs e)
    {
    if (e.KeyCode == Keys.Enter)
    {
    editorEnter = true;
    }
    }

    private void CopyCellFromPrevRow(GridViewRowInfo from, GridViewRowInfo to)
    {
    if (!(from != null && to != null && from != to))
    return;
    // copy cell by cell
    foreach (GridViewCellInfo cell in from.Cells)
    {
    var valPrev = cell.Value;
    var valCurr = to.Cells[cell.ColumnInfo.Name].Value;
    if (valPrev != null && valCurr == null)
    to.Cells[cell.ColumnInfo.Name].Value = valPrev;
    }
    }

    </code>

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Dimitar
    Admin
    Dimitar avatar
    1411 posts

    Posted 17 Aug 2015 Link to this post

    Hello Andrea,

    Thank you for writing.

    Actually pressing Enter in the new row will add it to the grid and you do not need to check if Enter is pressed. This will trigger UserAddedRow event and you just need to copy the cells. For example, the following code is working fine on my side:
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
            for (int i = 0; i < 4; i++)
            {
                radGridView1.Columns.Add("col" + i);
      
            }
            radGridView1.Rows.Add("cell 1", "cell 2", "cell 3", "cell 4");
            radGridView1.UserAddedRow += radGridView1_UserAddedRow;
        }
      
        void radGridView1_UserAddedRow(object sender, Telerik.WinControls.UI.GridViewRowEventArgs e)
        {
            GridViewRowInfo from = null;
            if (radGridView1.GridNavigator.SelectRow(e.Row))
            {
                if (radGridView1.GridNavigator.SelectPreviousRow(1))
                    from = radGridView1.CurrentRow;
            }
            CopyCellFromPrevRow(from, e.Row);
            radGridView1.GridNavigator.SelectRow(e.Row);
            radGridView1.GridNavigator.SelectNextRow(1);
            radGridView1.GridNavigator.SelectFirstColumn();
        }
      
        private void CopyCellFromPrevRow(GridViewRowInfo from, GridViewRowInfo to)
        {
            if (!(from != null && to != null && from != to))
                return;
            // copy cell by cell
            foreach (GridViewCellInfo cell in from.Cells)
            {
                var valPrev = cell.Value;
      
                var valCurr = to.Cells[cell.ColumnInfo.Name].Value;
                if (valPrev != null && valCurr == null)
                    to.Cells[cell.ColumnInfo.Name].Value = valPrev;
            }
        }
    }

    I hope this helps.

    Regards,
    Dimitar
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top