Click here to Skip to main content
15,867,934 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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:
C#
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

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.
C#
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.
 
Share this answer
 
v3
Comments
Mani Zachariah 19-Nov-12 1:56am    
but im developing a windows application.
Mohd. Mukhtar 19-Nov-12 1:59am    
In that case you need to update the the order by coustomising.
Mohd. Mukhtar 19-Nov-12 2:13am    
plese see the updated code.
Mani Zachariah 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 20-Nov-12 3:25am    
Hi Mani,

Since there is only few records, that does'nt make any performance difference.
C#
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);
                    }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900