我想把GRIDVIEW里面的数据删一条记录,ID号也减一,如:本来ID是1,2,3.把第二条记录删掉,3变成2,即1,2
str = "SELECT * FROM 申请表 where 票号='" & ViewState("AB1") & "' "
Dim SQLDP As SqlDataAdapter = New SqlDataAdapter(str, CONN) Dim DS As New DataSet
SQLDP.Fill(DS, "申请表")
For I = 1 To (DS.Tables("申请表").Rows.Count)
If I > X Then
str = "update 申请表 set ID = ID-1 "
Dim sqlcmd1 As New SqlCommand(str, CONN)
CONN.Open()
sqlcmd1.ExecuteNonQuery()
CONN.Close()
Else
Exit For
End If
Next I 这样写,ID没有变
str = "SELECT * FROM 申请表 where 票号='" & ViewState("AB1") & "' "
Dim SQLDP As SqlDataAdapter = New SqlDataAdapter(str, CONN) Dim DS As New DataSet
SQLDP.Fill(DS, "申请表")
For I = 1 To (DS.Tables("申请表").Rows.Count)
str = "update 申请表 set ID = ID-1 "
Dim sqlcmd1 As New SqlCommand(str, CONN)
CONN.Open()
sqlcmd1.ExecuteNonQuery()
CONN.Close()
Next I
把if去掉,ID改为0,2,即都减了1 怎样让它变为1,2
str = "SELECT * FROM 申请表 where 票号='" & ViewState("AB1") & "' "
Dim SQLDP As SqlDataAdapter = New SqlDataAdapter(str, CONN) Dim DS As New DataSet
SQLDP.Fill(DS, "申请表")
For I = 1 To (DS.Tables("申请表").Rows.Count)
If I > X Then
str = "update 申请表 set ID = ID-1 "
Dim sqlcmd1 As New SqlCommand(str, CONN)
CONN.Open()
sqlcmd1.ExecuteNonQuery()
CONN.Close()
Else
Exit For
End If
Next I 这样写,ID没有变
str = "SELECT * FROM 申请表 where 票号='" & ViewState("AB1") & "' "
Dim SQLDP As SqlDataAdapter = New SqlDataAdapter(str, CONN) Dim DS As New DataSet
SQLDP.Fill(DS, "申请表")
For I = 1 To (DS.Tables("申请表").Rows.Count)
str = "update 申请表 set ID = ID-1 "
Dim sqlcmd1 As New SqlCommand(str, CONN)
CONN.Open()
sqlcmd1.ExecuteNonQuery()
CONN.Close()
Next I
把if去掉,ID改为0,2,即都减了1 怎样让它变为1,2
支持!
Dim I As Integer
Dim X As Integer
X = Val(GridView1.DataKeys(e.RowIndex).Value.ToString)
str = "delete from [申请表] where [票号]='" & ViewState("AB1") & "' and ID='" & GridView1.DataKeys(e.RowIndex).Value.ToString & "'"
Dim SQLCMD As SqlCommand = New SqlCommand(str, CONN)
CONN.Open()
SQLCMD.ExecuteNonQuery()
CONN.Close()
str = "SELECT * FROM 申请表 where 票号='" & ViewState("AB1") & "' and id>'" & X & "' "
Dim SQLDP As SqlDataAdapter = New SqlDataAdapter(str, CONN) Dim DS As New DataSet
SQLDP.Fill(DS, "申请表")
For I = X + 1 To (DS.Tables("申请表").Rows.Count) str = "update 申请表 set ID = ID - 1 where id > '" & X & "'" Dim sqlcmd1 As New SqlCommand(str, CONN)
sqlcmd1.Connection.Open()
sqlcmd1.ExecuteNonQuery()
sqlcmd1.Connection.Close()
Next I
试过了,不行
一、没有分页的情况:
<asp:TemplateField HeaderText="序号">
<ItemTemplate>
<%# Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
二、考滤分页的情况:
<asp:TemplateField HeaderText="序号">
<ItemTemplate>
<%# this.GridView1.PageIndex * this.GridView1.PageSize + Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>不过楼主的意思如果不是为了增加一个序号列效果的话,那就当我没说吧。希望对你有帮助。
如果是的话,我再提供实现的方法:
http://www.cnblogs.com/insus/articles/1406440.html
二、我用的是vb2005.net
O(∩_∩)O谢谢你
同理,For I = 1 To (DS.Tables("申请表").Rows.Count) 这句话也只会循环一次。
如果想批量替换表中的ID,使其从某个ID号开始自动提升1位,是应该是这样写:
str = "update 申请表 "set ID = ID-1 where ID >= X"