重新排列网格中的数字 [英] Reordering the number in grid

查看:79
本文介绍了重新排列网格中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个网格重新排序的小问题.让我解释一下,让datagridview添加配方成分,当添加每种成分时,优先级编号将被分配为1,2,3 ...等.所有这些都很好但我真正的问题是用户可以编辑优先级并更改该值,将显示例如:
没有成分
1牛奶
2个鸡蛋
3水
4糖
我的客户告诉我,如果他更改no,即假设他用我的代码将糖优先级从4更改为2,它将交换位置,即o/p将像这样
没有成分
1牛奶
2糖
3水
4个鸡蛋

但他想像这样将行向下移
没有成分
1牛奶
2糖
3个鸡蛋
4水
任何想法都会帮助我2 ...
我使用的代码是:

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;
        }

推荐答案

玛尼,

您应该使用AJAX重新排序列表.它的实现既简单又非常用户友好.

使用下面的链接来实现相同的功能.

使用ASP.NET AJAX控件工具包的ReorderList控件和LINQ进行实时重新排序到SQL [ ^ ]

或使用以下代码更新订单.

更新给定项目的位置并找到该项目的当前位置

例如.
该项目的当前位置是5.
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
  }
}

希望这对您有所帮助.


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);
                    }


这篇关于重新排列网格中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆