使用datagridview复选框删除多于一行的问题. [英] problem to delete more then one row using datagridview checkbox.

查看:100
本文介绍了使用datagridview复选框删除多于一行的问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用vs2010.
我有一个问题.当我使用datagridview的复选框从后端数据库删除多行时,它会在后端维护我的字段NO .
但是假设,当我删除第2行和第4行时,它将无法维护后端.我的编程是,当我删除一行时,所有的行都将不再存在.No field Number就像n = n-1
我的代码在这里.有人可以解决吗?

I am using vs 2010.
I have one problem. When I delete more then one row from backend database using datagridview''s checkbox it maintains a my field NO in backend.
But suppose, when I delete a row 2 and 4 then it can not maintain a backend. My programming is that when I delete a row then all then getherthan of that rows No field Number is like n=n-1
My code is here. Can anyone solve it?

Dim i, j, n As Integer
       i = 0
       j = 0
       Dim x As Integer = 0
       Dim a(1000) As String
       a(x) = ""
       Try
           While i < DataGridView1.Rows.Count
               If DataGridView1.Rows(i).Cells(0).Value() Then
                   n = DataGridView1.Rows(i).Cells(1).Value()
                   Call openconnection()
                   'Here It Will Delete your Row From The Database Depending On Your Id....
                   str = "DELETE FROM Assets_Detail Where   No=" & n & ""
                   cmd = New SqlCommand(str, cn)
                   cmd.ExecuteNonQuery()
                   a(x) = n.ToString

                   cn.Close()
                   x = x + 1
               End If
               i = i + 1
           End While
       Catch ex As Exception
           MsgBox(ex.ToString())
       End Try
       While j <= Val(a(x))
           Dim temp As Integer
           temp = Val(a(x))
           Call openconnection()
           str = "update Assets_Detail set No=No-1 where No > " & temp & ""
           cmd = New SqlCommand(str, cn)
           cmd.ExecuteNonQuery()
           cn.Close()

           j = j + 1
       End While

推荐答案

出了什么问题?
似乎您想使用以下方式删除中间的行后再维护序列号:
What is the problem?
It seems like you want to maintain then sequence number after deleting the rows in the middle using:
While j <= Val(a(x))
    Dim temp As Integer
    temp = Val(a(x))
    Call openconnection()
    str = "update Assets_Detail set No=No-1 where No > " & temp & ""
    cmd = New SqlCommand(str, cn)
    cmd.ExecuteNonQuery()
    cn.Close()
    j = j + 1
End While



那是行不通的.

如果是这种情况,那么:
您必须按降序对a()中的值进行排序,然后遍历a()并使用更新查询.
顺便说一句,通常使用数字作为主键,并且不必担心要删除的项目.



And that is not working.

If this is the case then:
You have to sort the values in a() in descending order and then loop through a() and use your update query.
BTW, normally use a number as primary key, and don''t bother about the items being deleted.


这篇关于使用datagridview复选框删除多于一行的问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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