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

Up and Down buttons

1 Answer 249 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Art
Top achievements
Rank 1
Art asked on 04 Oct 2015, 01:31 AM

Has anyone created a good way to have up and down button for a grid.

I have a grid with a list of items that my user would want to order. When they click the Up button the grids current row moves up one row, trading placed with the row above it. Of course the opposite is true with the down button.

 I'm trying to use a "sort_order" column with numbers 1 through the childrow count. The buttons' click event edits the current gridrow and the one above it, changing their sort_order field accordingly. However, I'm running into problems when setting the column values. It seems as though I can change the current row, but when I change the previous row the current row's cell value changes back and the previous row doesn't change.

Any ideas .. really, any ideas at all.

Later

Art

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 07 Oct 2015, 09:44 AM
Hello Art,

Thank you for writing.

RadGridView manages user mouse and keyboard input over its rows by GridRowBehavior. Depending on the row type, RadGridView introduces different behaviors. If you need to customize the default behavior of the up/down arrow keys, you can create a derivative of the GridDataRowBehavior and override its ProcessUpKey and ProcessDownKey methods. Thus, you can perform rows reordering. Note that mode you should reorder the data items in the source collection, not to reorder the associated grid rows. You can refer to Row behaviors help article for additional information. I have prepared a sample code snippet which result is illustrated on the attached gif file:
public Form1()
{
    InitializeComponent();
 
    List<Item> items = new List<Item>();
    for (int i = 0; i < 10; i++)
    {
        items.Add(new Item(i,"Item" + i));
    }
    this.radGridView1.DataSource = items;
    this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
     
    BaseGridBehavior gridBehavior = radGridView1.GridBehavior as BaseGridBehavior;
    gridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo));
    gridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo), new CustomGridDataRowBehavior());
}
 
public class CustomGridDataRowBehavior : GridDataRowBehavior
{
    protected override bool ProcessUpKey(KeyEventArgs keys)
    {
        List<Item> items = this.GridControl.DataSource as List<Item>;
        if (items != null && this.GridControl.CurrentRow != null)
        {
            Item currentItem = this.GridControl.CurrentRow.DataBoundItem as Item;
            int index = Math.Max(0, items.IndexOf(currentItem) - 1);
            this.GridControl.DataSource = null;
            items.Remove(currentItem);
            items.Insert(index, currentItem);
            this.GridControl.DataSource = items;
            this.GridControl.CurrentRow = this.GridControl.Rows[index];
        }
 
        return true;
    }
 
    protected override bool ProcessDownKey(KeyEventArgs keys)
    {
        List<Item> items = this.GridControl.DataSource as List<Item>;
        if (items != null && this.GridControl.CurrentRow != null)
        {
            Item currentItem = this.GridControl.CurrentRow.DataBoundItem as Item;
            int index = Math.Min(items.Count - 1, items.IndexOf(currentItem) + 1);
            this.GridControl.DataSource = null;
            items.Remove(currentItem);
            items.Insert(index, currentItem);
            this.GridControl.DataSource = items;
            this.GridControl.CurrentRow = this.GridControl.Rows[index];
        }
 
        return true;
    }
}
 
public class Item
{
    public int Id { get; set; }
 
    public string Title { get; set; }
 
    public Item(int id, string title)
    {
        this.Id = id;
        this.Title = title;
    }
}

Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify it in a way which suits your requirement best.
 
I hope this information helps. Should you have further questions I would be glad to help.
 
Regards,
Dess
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
Tags
GridView
Asked by
Art
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or