Click here to Skip to main content
Sign Up to vote bad
good
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:43


2 solutions

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  
Comments
Mani Zachariah - 19 Nov '12 - 1:56
but im developing a windows application.
Mohd. Mukhtar - 19 Nov '12 - 1:59
In that case you need to update the the order by coustomising.
Mohd. Mukhtar - 19 Nov '12 - 2:13
plese see the updated code.
Mani Zachariah - 19 Nov '12 - 22:55
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 - 20 Nov '12 - 3:25
Hi Mani, Since there is only few records, that does'nt make any performance difference.
Mani Zachariah - 19 Nov '12 - 23:00
we will get the previous value from cell click event.Please have your comment.
Mani Zachariah - 20 Nov '12 - 4:31
Hi Mohd.Mukhtar k thank u..
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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 508
1 Arun Vasu 275
2 Maciej Los 238
3 OriginalGriff 215
4 Mahesh Bailwal 200
0 Sergey Alexandrovich Kryukov 9,660
1 OriginalGriff 7,329
2 CPallini 3,968
3 Rohan Leuva 3,339
4 Maciej Los 2,851


Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid