How to shift rows from one index to another index?

2 Answers 107 Views
GridView
Shubham
Top achievements
Rank 3
Iron
Iron
Iron
Shubham asked on 05 Jul 2023, 10:45 AM | edited on 05 Jul 2023, 10:47 AM

I want to achieve functionality of Re-Ordering Rows In a Grid.

Requirement:
User will Select Row and Click on Up Button on UI to shift that on Up direction. Example If user select 3rd row from top and then click on up then it must shift that row to 2 position from top.
Also the which was on 2nd postion from top now comes on 3rd from top 

Below code I tried but not working Properly When Rows are More than 2 in a grid:

private void btnInternalUp_Click(object sender, EventArgs e)
    {

for (int index = 0; index < this.m_grdGridControl.SelectedRows.Count; index++)
{
var currentRow = this.m_grdGridControl.SelectedRows[index];
int currentIndex = currentRow.Index;
int nextIndex = NextIndex(this.m_grdGridControl, currentIndex, "up");                    
this.m_grdGridControl.MasterTemplate.Rows.Move(currentIndex, nextIndex);
   
}

    }

//NextIndexMethod Return the index where row need to be shifted:

public static int NextIndex(RadGridView grd,int currentIndex,string direction)
    {
        int nextIndex;
        if (direction.ToLower() == "up")
        {
            if(currentIndex == 0)
            {
                nextIndex = 0;
            }
            else
            {
                nextIndex = currentIndex - 1;
            }
        }
        else
        {
            if(currentIndex == grd.Rows.Count - 1)
            {
                nextIndex = currentIndex;
            }
            else
            {
                nextIndex = currentIndex + 1;
            }
        }

        return nextIndex;
    }

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Shubham
Top achievements
Rank 3
Iron
Iron
Iron
answered on 10 Jul 2023, 05:36 AM

Hello All,
Below mentioned is the piece of code which works for me and I hope it would helpful for others as well so sharing my code.

/---Code---

//this.m_grdGridControl this is 

 if (this.m_grdGridControl.SelectedRows.Count > 0)
            {
                for (int i = 0; i < this.m_grdGridControl.SelectedRows.Count; i++)
                {
                    var currentRow = this.m_grdGridControl.SelectedRows[i];
                    int currentIndex = currentRow.Index;
                    int nextIndex = NextIndex(this.m_grdGridControl, currentIndex, "down");
                    this.m_grdGridControl.Rows.Move(currentIndex, nextIndex);
                }

            }

//Next Index Method will decide destination index of selected current row

private int NextIndex(RadGridView grd, int currentIndex, string direction)
    {
        int nextIndex;
        if (direction.ToLower() == "up")
        {
            if (currentIndex == 0)
            {
                nextIndex = 0;
            }
            else
            {
                nextIndex = currentIndex - 1;
            }
        }
        else
        {
            if (currentIndex == grd.Rows.Count - 1)
            {
                nextIndex = currentIndex;
            }
            else
            {
                nextIndex = currentIndex + 1;
            }
        }

        return nextIndex;
    }

Dess | Tech Support Engineer, Principal
Telerik team
commented on 11 Jul 2023, 11:22 AM

Hi, Shubham,

I am glad that you have found a suitable solution that works for your scenario. I have reviewed the provided code snippet and it seems OK to me. As to the previously referred forum thread, could you please specify why this approach is not applicable for your case? It also swaps the cells' values and thus moves the rows.

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 05 Jul 2023, 12:58 PM

Hello,

I believe that the following forum post would be quite useful on achieving your goal since it shows a sample approach how to do it:

https://www.telerik.com/forums/moving-rows-up-and-down-in-gridview 

Please give it a try and see how it works on your end.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Shubham
Top achievements
Rank 3
Iron
Iron
Iron
commented on 07 Jul 2023, 05:47 AM | edited

Hello Dess,

Above shared resource was not helpful for me can you share me some other ways.
It would be really a great help I searched lot on forum but not found anything which works for me.

Note: I am using GridViewComboBoxColumns in my grid


Thanks,
Shubham Jain

Tags
GridView
Asked by
Shubham
Top achievements
Rank 3
Iron
Iron
Iron
Answers by
Shubham
Top achievements
Rank 3
Iron
Iron
Iron
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or