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

Move rad gridview data up and down

3 Answers 330 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mohan
Top achievements
Rank 1
Mohan asked on 10 Dec 2013, 06:56 AM
Hi,
I have a gridview loaded with a datatable.  I have 2 buttons "Up" and "Down" outside the grid.  The "Up" button takes the selected row and moves it up one row.  The "Down" button takes the selected row down a row. 

eg:
1  sample1
2  sample2
if selected row is sample1 and then i click down button, i need to change the grid row to
2 sample2
1  sample1

please give solution for this

3 Answers, 1 is accepted

Sort by
0
Abdul
Top achievements
Rank 1
answered on 23 Jun 2017, 04:41 AM

Hi ,

I need to implement the row move up and down in RadGridView using RadButtons.

Regards
ABDUL HAFEEL

0
Abdul
Top achievements
Rank 1
answered on 23 Jun 2017, 06:08 AM

Hi,

I got Solution.

public static void MoveRowUp(BindingSource sender)
        {
            if (!(string.IsNullOrWhiteSpace(sender.Sort)))
                sender.Sort = "";
            int NewIndex = Convert.ToInt32((sender.Position == 0) ? 0 : sender.Position - 1);
            var dt = (DataTable)sender.DataSource;
            DataRow RowToMove = ((DataRowView)sender.Current).Row;
            DataRow NewRow = dt.NewRow();
            NewRow.ItemArray = RowToMove.ItemArray;
            dt.Rows.RemoveAt(sender.Position);
            dt.Rows.InsertAt(NewRow, NewIndex);
            dt.AcceptChanges();
            sender.Position = NewIndex;
        }
        public static void MoveRowDown(BindingSource sender)
        {
            if (!(string.IsNullOrWhiteSpace(sender.Sort)))
                sender.Sort = "";
            int UpperLimit = sender.Count - 1;
            int NewIndex = Convert.ToInt32((sender.Position + 1 >= UpperLimit) ? UpperLimit : sender.Position + 1);
            var dt = (DataTable)sender.DataSource;
            DataRow RowToMove = ((DataRowView)sender.Current).Row;
            DataRow NewRow = dt.NewRow();
            NewRow.ItemArray = RowToMove.ItemArray;
            dt.Rows.RemoveAt(sender.Position);
            dt.Rows.InsertAt(NewRow, NewIndex);
            dt.AcceptChanges();
            sender.Position = NewIndex;
        }

 

Here I am binding data into RadGridView.Then passing binding source to the function.

Regards

ABDUL HAFEEL

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 26 Jun 2017, 11:56 AM
Hello guys, 

Thank you for writing.  

In order to reorder the rows in RadGridView, it is necessary to reorder the records in the DataSource accordingly. A sample approach is demonstrated in the following help article using the drag and drop behavior: http://docs.telerik.com/devtools/winforms/gridview/rows/drag-and-drop
You can use a similar approach for achieving the same functionality with buttons.

@Abdul, I have updated your Telerik points for the shared solution.

I hope this information helps. Should you have further questions I would be glad to help.

Regards,
Dess
Progress Telerik
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.
Tags
GridView
Asked by
Mohan
Top achievements
Rank 1
Answers by
Abdul
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or