删除并重新排序datagridview中的序列号 [英] Remove and reorder the serial number in datagridview

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

问题描述

我想知道在c#windows应用程序中从datagridview删除行时如何重新排序序列号的编码。以及如何通过在datagridview中选择它来删除行。请注意,datagrid视图与数据库连接。例如。



Sr. No. City

1 Delhi

2 Mumbai

3 Chennai

删除Sr. No. 2后,序列号3替换它并显示sr no2而不是3.



Sr 。号城市

1德里

2钦奈



我尝试过:



i试过这个但是我无法做到这一点

i want to know the coding about how to reorder the serial number when we delete a row from datagridview in c# windows application. and also how to delete a row by just selecting it in datagridview. note that datagrid view is connected with database. eg.

Sr. No. City
1 Delhi
2 Mumbai
3 Chennai
After deleting the Sr. No. 2 the serial number 3 replace it and show sr no2 instead of 3.

Sr. No. City
1 Delhi
2 Chennai

What I have tried:

i have tried this but i am unable to do this

推荐答案

这不是你想要的几乎可以肯定。

DB列可能被设置为IDENTITY字段 - 这意味着数据库会对其进行维护并确保不重复这些值。这些数字永远不应该被改变,而是留给SQL。

其原因与你根本不应该这样做的原因相同:当数据库的其他地方几乎肯定会引用它们。你使用城市代码来防止重复 - 如果你开始重新组织数字,那么该表中的每个记录都会更新,这会导致两个问题:

1)如果任何其他表引用那个通过外键 - 因为问题应该,并且将 - 然后突然间关系被打破,因为最高价值突然不再存在。 SQL不会让这种情况发生,因此更改将被拒绝。

2)更糟糕的是,如果你删除孟买,那么所有通过Sr.No2引用孟买的东西突然发现自己被记录下来就像在钦奈。这很糟糕,非常糟糕,因为一旦你做出这样的改变,需要大量的手动修复数据,特别是如果你没有立即注意到这一点。



不要这样做:如果你想要序列号,请使用 ROWNUMBER()OVER 来提供 - 反映当前状态的临时值。不要试图将现有的ID值改为看起来整洁 - 它总会导致比解决的问题更多的问题。
That's not what you want to do, almost certainly.
The chances are that the DB column is set as an IDENTITY field - which means that the database maintains it and ensures that values aren't duplicated. These numbers should never be changed but left up to SQL.
And the reason for that is the same as the reason you shouldn't do it at all: those are almost certainly referenced elsewhere in your database when you use the city code to prevent duplication - if you start reorganising the numbers, then every record in that table will get updated, and that leads to two problems:
1) If any other table refers to that via a foreign key - as it problem should, and will - then suddenly the relationship is broken because the "highest value" suddenly no longer exists. SQL won't let that happen, so the changes will get rejected.
2) Worse, if you delete Mumbai, then everything which referred to Mumbai via the Sr.No "2" suddenly finds themselves recorded as being in Chennai. That's bad, very bad, because it takes a huge amount of manual effort to fix the data once you commit a change like that, particularly if you don't notice immediately.

Don't do it: if you want sequential numbers, use ROWNUMBER() OVER to give you just that - temporary values which reflect the current status. Don't try to alter existing ID values to "look tidy" - it always causes more problems than it solves.


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

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