Click here to Skip to main content
11,707,470 members (69,877 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi,

I'm having a small prob with grid reordering.Let me explain,got datagridview for adding the ingredients for recipe ,when adding each ingredients a priority number will be assigned like 1,2,3...etc.All these works fine but my real prob is that the user can edit the priority and change the value will show an eg:
No Ingredient
1 Milk
2 Egg
3 Water
4 Sugar
My client told that if he changes the no i.e suppose he is changing the sugar priority from 4 to 2 with my code it will swap the position i.e the o/p will be like this
No Ingredient
1 Milk
2 Sugar
3 Water
4 Egg

but he wants to shift the row downwards like this
No Ingredient
1 Milk
2 Sugar
3 Egg
4 Water
any idea will be grea help 2 me...
the code im using is:
if (e.RowIndex >= 0 && proceed == 1 && e.ColumnIndex == 7)
               {
                   int currentvalue = dg_ingredients[e.ColumnIndex, e.RowIndex].Value.ToString().ToInt();
                   foreach (DataGridViewRow dg_ingredientsRow in dg_ingredients.Rows)
                   {
                       if (dg_ingredientsRow.Cells[7].Value.ToString().ToInt() == currentvalue && (dg_ingredientsRow.Index != e.RowIndex))
                       {
                           dg_ingredientsRow.Cells[7].Value = e.RowIndex + 1;
                           break;
                       }
                   }
                   dg_ingredients.Sort(new RowComparer(SortOrder.Ascending));
                   reorderlist(7);
               }
function reorder:
void reorderlist(int cell_index)
        {
            proceed = 0;
            int stepnumber = 1;
            DataGridView dg = cell_index== 1? dg_directions: dg_ingredients;
            foreach (DataGridViewRow dg_Row in dg.Rows)
            {
                dg_Row.Cells[cell_index].Value = stepnumber;
                stepnumber++;
            }
            proceed = 1;
        }
Posted 18-Nov-12 19:43pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi Mani,

You should use AJAX reorder list. Its implementation is easy and very user friendly.

Use the below link to implement the same.

Realtime Reordering Using the ASP.NET AJAX Control Toolkit’s ReorderList Control with LINQ to SQL[^]

OR use the below code to update order.

update the position of the given item and find current position of that item

like for example.
current position of the item is 5.
currPosition = 5;
uptadedPosition = 2;
if(uptadedPosition > currPosition) 
{
  for(currPosition; currPosition< (updatedPosition-1); currPositon++)
  {
    //decrement the current order by 1
  } 
}
else if(uptadedPosition < currPosition) 
{
  updadedPosition--;
  for(updadedPosition; updadedPosition < (currPosition -1); updadedPosition++)   
  {  
    //increment the current order of current row by 1
  }
}
Hope this will help you.
  Permalink  
v3
Comments
Mani Zachariah at 19-Nov-12 1:56am
   
but im developing a windows application.
Mohd. Mukhtar at 19-Nov-12 1:59am
   
In that case you need to update the the order by coustomising.
Mohd. Mukhtar at 19-Nov-12 2:13am
   
plese see the updated code.
Mani Zachariah at 19-Nov-12 22:55pm
   
Hi Mohd. Mukhtar,

Got another logic can u just see how much feasible it is?Its working fine but will there be any performance issues
Mohd. Mukhtar at 20-Nov-12 3:25am
   
Hi Mani,

Since there is only few records, that does'nt make any performance difference.
Mani Zachariah at 19-Nov-12 23:00pm
   
we will get the previous value from cell click event.Please have your comment.
Mani Zachariah at 20-Nov-12 4:31am
   
Hi Mohd.Mukhtar
k thank u..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

int currentvalue = dg_ingredients[e.ColumnIndex, e.RowIndex].Value.ToString().ToInt();
                    index = dg_ingredients.CurrentRow.Index;
                    if (currentvalue <= dg_ingredients.Rows.Count && (currentvalue - 1) != -1 && _previousvalue < currentvalue)
                    {
                        dg_ingredients.Rows.Insert(currentvalue, dg_ingredients.Rows[index].Cells[0].Value, dg_ingredients.Rows[index].Cells[1].Value, dg_ingredients.Rows[index].Cells[2].Value, dg_ingredients.Rows[index].Cells[3].Value, dg_ingredients.Rows[index].Cells[4].Value, dg_ingredients.Rows[index].Cells[5].Value, dg_ingredients.Rows[index].Cells[6].Value, dg_ingredients.Rows[index].Cells[7].Value);
                        dg_ingredients.Rows.RemoveAt(index);
                    }
                    else
                    {
                        dg_ingredients.Rows.Insert((currentvalue-1), dg_ingredients.Rows[index].Cells[0].Value, dg_ingredients.Rows[index].Cells[1].Value, dg_ingredients.Rows[index].Cells[2].Value, dg_ingredients.Rows[index].Cells[3].Value, dg_ingredients.Rows[index].Cells[4].Value, dg_ingredients.Rows[index].Cells[5].Value, dg_ingredients.Rows[index].Cells[6].Value, dg_ingredients.Rows[index].Cells[7].Value);
                        dg_ingredients.Rows.RemoveAt(index+1);
                    }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 195
1 Mika Wendelius 190
2 OriginalGriff 156
3 Sergey Alexandrovich Kryukov 145
4 chainerlt 110
0 OriginalGriff 9,178
1 Sergey Alexandrovich Kryukov 8,562
2 CPallini 5,189
3 Maciej Los 4,806
4 Mika Wendelius 3,706


Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100