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

Moving Rows Up and Down in GridView

2 Answers 728 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Gopinath
Top achievements
Rank 1
Gopinath asked on 11 Apr 2017, 06:05 AM

Hi

How can i move rows up and down in the grid, by pressing UP Arrow and DownArrow Key. Is there any way to do this?

Regards

Gopinath

 

2 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 12 Apr 2017, 01:43 PM
Hi Gopinath,

Thank you for writing.

You can accomplish your task by creating a custom row behavior which will override the default implementation of how the Up and Down keys are handled: Row Behavior.

Basically, you will need to override the ProcessKey method in the custom behavior class and swap the cell values. The solution below handles both bound and unbound modes: 
public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
    private DataTable dt;
 
    public RadForm1()
    {
        InitializeComponent();
 
        this.radGridView1.DataSource = this.GetData();
        this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
        BaseGridBehavior gridBehavior = radGridView1.GridBehavior as BaseGridBehavior;
        gridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo));
        gridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo), new CustomGridDataRowBehavior());
    }
 
    private DataTable GetData()
    {
        dt = new DataTable();
 
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Date", typeof(DateTime));
        dt.Columns.Add("Bool", typeof(bool));
        for (int i = 0; i < 500; i++)
        {
            dt.Rows.Add(i, "Name " + i, DateTime.Now.AddDays(i), i % 2 == 0);
        }
 
        return dt;
    }
}
 
public class CustomGridDataRowBehavior : GridDataRowBehavior
{
    public override bool ProcessKey(KeyEventArgs keys)
    {
        if (keys.KeyCode == Keys.Up || keys.KeyCode == Keys.Down)
        {
            var oldCurrent = this.GridControl.CurrentRow;
            bool res = base.ProcessKey(keys);
            var newCurrent = this.GridControl.CurrentRow;
 
            if (newCurrent != null && newCurrent.Index > -1)
            {
                for (int i = 0; i < oldCurrent.Cells.Count; i++)
                {
                    object oldValue = oldCurrent.Cells[i].Value;
                    object newValue = newCurrent.Cells[i].Value;
                    newCurrent.Cells[i].Value = oldValue;
                    oldCurrent.Cells[i].Value = newValue;
                }
            }
 
            return res;
        }
 
        return base.ProcessKey(keys);
    }
}

I am also attaching a short video showing the result on my end.

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Gopinath
Top achievements
Rank 1
answered on 15 Apr 2017, 05:46 AM

Hi Hristo

Thanks for the same, its working food.

Regards

Gopinath

 

 

Tags
GridView
Asked by
Gopinath
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Gopinath
Top achievements
Rank 1
Share this question
or