| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 827 人关注过本帖
标题:[求助]datagrid中数据 的删除问题
只看楼主 加入收藏
mql
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2006-4-21
收藏
 问题点数:0 回复次数:3 
[求助]datagrid中数据 的删除问题

我在datagrid上删除一条记录的时候,本来是按照一定的查询条件显示信息的,可是我一删除其中的记录
datagrid将数据库中的信息全部显示出来了。我的查询条件就没有用了。我发现是我的一个Sub binddatagrid()方法有错误,我就设置了条件。
可是我设置的条件没有用,我定义的i不能传来值,请各位大哥替我改改这个程序。
Imports System.Data
Imports System.Data.SqlClient
Public Class managerchaxun
Inherits System.Web.UI.Page

#Region " Web 窗体设计器生成的代码 "

'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents totxt As System.Web.UI.WebControls.TextBox
Protected WithEvents Label7 As System.Web.UI.WebControls.Label
Protected WithEvents fromtxt As System.Web.UI.WebControls.TextBox
Protected WithEvents Label6 As System.Web.UI.WebControls.Label
Protected WithEvents Label5 As System.Web.UI.WebControls.Label
Protected WithEvents Button3 As System.Web.UI.WebControls.Button
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents TextBox3 As System.Web.UI.WebControls.TextBox
Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Label3 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents Textbox4 As System.Web.UI.WebControls.TextBox
Protected WithEvents Label4 As System.Web.UI.WebControls.Label
Protected WithEvents Button4 As System.Web.UI.WebControls.Button
Protected WithEvents Button5 As System.Web.UI.WebControls.Button
Protected WithEvents Label8 As System.Web.UI.WebControls.Label
Protected WithEvents txtfrom As System.Web.UI.WebControls.TextBox
Protected WithEvents Label9 As System.Web.UI.WebControls.Label
Protected WithEvents Button6 As System.Web.UI.WebControls.Button
Protected WithEvents Label11 As System.Web.UI.WebControls.Label
Protected WithEvents txtto As System.Web.UI.WebControls.TextBox
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label10 As System.Web.UI.WebControls.Label
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents Label12 As System.Web.UI.WebControls.Label

'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub

#End Region
Dim i As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New sqlconnection
conn.ConnectionString = ConfigurationSettings.AppSettings("manager")
conn.Open()
Dim cmd As New SqlCommand

If TextBox1.Text = "" Then
Label5.Text = "请输入你要查询的字段!"
Else
i = 1
cmd.CommandText = "select * from teacher where t_name like'%" & TextBox1.Text & "%'"
cmd.Connection = conn
cmd.ExecuteNonQuery()
Dim dr As New DataSet
Dim adp As New SqlDataAdapter(cmd)
adp.Fill(dr, "teacher")

Dim reader As SqlDataReader
reader = cmd.ExecuteReader()
DataGrid1.DataSource = dr
DataGrid1.DataBind()
Textbox4.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
txtfrom.Text = ""
txtto.Text = ""
fromtxt.Text = ""
totxt.Text = ""
conn.Close()
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim conn As New SqlConnection
conn.ConnectionString = ConfigurationSettings.AppSettings("manager")
conn.Open()
Dim cmd As New SqlCommand
If TextBox2.Text = "" Then
Label5.Text = "请输入你要查询的字段!"
Else
i = 2
cmd.CommandText = "select * from teacher where t_id like'%" & TextBox2.Text & "%'"

cmd.Connection = conn
cmd.ExecuteNonQuery()
Dim dr As New DataSet
Dim adp As New SqlDataAdapter(cmd)
adp.Fill(dr, "teacher")

Dim reader As SqlDataReader
reader = cmd.ExecuteReader()
DataGrid1.DataSource = dr
DataGrid1.DataBind()
TextBox1.Text = ""
TextBox3.Text = ""
Textbox4.Text = ""
txtfrom.Text = ""
txtto.Text = ""
fromtxt.Text = ""
totxt.Text = ""
conn.Close()
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim conn As New SqlConnection
conn.ConnectionString = ConfigurationSettings.AppSettings("manager")
conn.Open()
Dim cmd As New SqlCommand
If TextBox3.Text = "" Then
Label5.Text = "请输入你要查询的字段!"
Else
i = 3

cmd.CommandText = "select * from teacher where creer like'%" & TextBox3.Text & "%'"

cmd.Connection = conn
cmd.ExecuteNonQuery()
Dim dr As New DataSet
Dim adp As New SqlDataAdapter(cmd)
adp.Fill(dr, "teacher")

' Dim reader As SqlDataReader
'reader = cmd.ExecuteReader()
DataGrid1.DataSource = dr
DataGrid1.DataBind()
TextBox1.Text = ""
TextBox2.Text = ""
Textbox4.Text = ""
txtfrom.Text = ""
txtto.Text = ""
fromtxt.Text = ""
totxt.Text = ""
conn.Close()
End If
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim conn As New SqlConnection
conn.ConnectionString = ConfigurationSettings.AppSettings("manager")
conn.Open()
Dim cmd As New SqlCommand
If Textbox4.Text = "" Then
Label5.Text = "请输入你要查询的字段!"
Else
i = 4
cmd.CommandText = "select * from teacher where sex like'%" & Textbox4.Text & "%'"

cmd.Connection = conn
cmd.ExecuteNonQuery()
Dim dr As New DataSet
Dim adp As New SqlDataAdapter(cmd)
adp.Fill(dr, "teacher")

' Dim reader As SqlDataReader
'reader = cmd.ExecuteReader()
DataGrid1.DataSource = dr
DataGrid1.DataBind()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
txtfrom.Text = ""
txtto.Text = ""
fromtxt.Text = ""
totxt.Text = ""
conn.Close()
End If
End Sub
Sub pagechanged(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
DataGrid1.CurrentPageIndex = e.NewPageIndex
'binddatagrid()
End Sub
『『『『『 ''Sub binddatagrid() 这是我没有修改以前的

'' Dim conn As New SqlConnection
'' conn.ConnectionString = ConfigurationSettings.AppSettings("manager")
'' conn.Open()
'' Dim cmd As New SqlCommand
'' cmd.CommandText = "select * from teacher "
'' cmd.Connection = conn
'' Dim dr1 As New DataSet
'' Dim adp As New SqlDataAdapter(cmd)
'' adp.Fill(dr1, "teacher")

'' DataGrid1.DataSource = dr1
'' DataGrid1.DataBind()
'' conn.Close()
'End Sub』』』』』』』』

Sub binddatagrid1() 问题就在这里。我设定的判定条件好像没有执行啊!
Dim conn As New SqlConnection
conn.ConnectionString = ConfigurationSettings.AppSettings("manager")
conn.Open()

Dim cmd As New SqlCommand
If i = 1 Then
cmd.CommandText = "select * from teacher where t_name like'%" & TextBox1.Text & "%'"
ElseIf i = 2 Then
cmd.CommandText = "select * from teacher where t_id like'%" & TextBox2.Text & "%'"
ElseIf i = 3 Then
cmd.CommandText = "select * from teacher where creer like'%" & TextBox3.Text & "%'"
ElseIf i = 4 Then
cmd.CommandText = "select * from teacher where sex like'%" & Textbox4.Text & "%'"
ElseIf i = 5 Then
cmd.CommandText = "select * from teacher where data between '" & fromtxt.Text & "' And '" & totxt.Text & "'"
ElseIf i = 6 Then
cmd.CommandText = "select * from teacher where birth between '" & txtfrom.Text & "' And '" & txtto.Text & "'"
End If

cmd.Connection = conn
Dim dr1 As New DataSet
Dim adp As New SqlDataAdapter(cmd)
adp.Fill(dr1, "teacher")

DataGrid1.DataSource = dr1
DataGrid1.DataBind()
conn.Close()
End Sub


Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

Dim conn As New SqlConnection
conn.ConnectionString = ConfigurationSettings.AppSettings("manager")
conn.Open()
Dim cmd As New SqlCommand

If fromtxt.Text <> "" And totxt.Text <> "" Then
i = 5
cmd.CommandText = "select * from teacher where data between '" & fromtxt.Text & "' And '" & totxt.Text & "'"
cmd.Connection = conn
cmd.ExecuteNonQuery()
Dim dr As New DataSet
Dim adp As New SqlDataAdapter(cmd)
adp.Fill(dr, "teacher")
Dim reader As SqlDataReader
reader = cmd.ExecuteReader()
DataGrid1.DataSource = dr
DataGrid1.DataBind()
Else
Label5.Text = "请输入查询字段!"
End If
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
Textbox4.Text = ""
txtfrom.Text = ""
txtto.Text = ""
conn.Close()

End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim conn As New SqlConnection
conn.ConnectionString = ConfigurationSettings.AppSettings("manager")
conn.Open()
Dim cmd As New SqlCommand

If txtfrom.Text <> "" And txtto.Text <> "" Then
i = 6
cmd.CommandText = "select * from teacher where birth between '" & txtfrom.Text & "' And '" & txtto.Text & "'"
cmd.Connection = conn
cmd.ExecuteNonQuery()
Dim dr As New DataSet
Dim adp As New SqlDataAdapter(cmd)
adp.Fill(dr, "teacher")
Dim reader As SqlDataReader
reader = cmd.ExecuteReader()
DataGrid1.DataSource = dr
DataGrid1.DataBind()
Else
Label5.Text = "请输入查询字段!"
End If
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
Textbox4.Text = ""
fromtxt.Text = ""
totxt.Text = ""
conn.Close()
End Sub

'删除
Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand
Dim conn As New SqlConnection
conn.ConnectionString = ConfigurationSettings.AppSettings("manager")
conn.Open()
Dim cmd As New SqlCommand("delete from teacher where t_id='" & e.Item.Cells(0).Text & "'")
cmd.Connection = conn
cmd.ExecuteNonQuery()

binddatagrid1() 在这里调用
conn.Close()

End Sub


End Class
请问我定义的i怎么能传过来啊!也就是在datagrid1中怎么能接收到我所定义的i那。帮忙给改一下。谢谢!!!!!!!!!

搜索更多相关主题的帖子: datagrid 数据 删除 
2006-04-24 10:17
zhangwei
Rank: 1
等 级:新手上路
威 望:1
帖 子:277
专家分:0
注 册:2005-5-3
收藏
得分:0 
(1).你要传参可以使用"ByVal i as Integer",然后调用 binddatagrid1(i).

(2).在你查询的的时候 i 的值己经改变,问题不是在 i ,而出在 TextBox 当你在DataGrid1_DeleteCommand方法中调用binddatagrid1()方法时textbox的值是空值,那么你使用的Like将查询出所有的记录.(你可以使用string变量存储Textbox的值)

(3).建议你的DataGrid1_DeleteCommand方法,不要使用binddatagrid1(),而
使用GridView.Item(?).Delete() 或 datatable.Rows.item(?).delect()


主动沟通......
2006-04-24 17:02
mql
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2006-4-21
收藏
得分:0 

这位大哥,能不能在说清楚一些啊。帮我改一下怎么样啊,谢了!

2006-04-25 15:47
chenblue
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2005-9-24
收藏
得分:0 
Sub binddatagrid1() 问题就在这里。我设定的判定条件好像没有执行啊!
改成
Sub binddatagrid1(ByVal i as Integer)

你可以设置是视图,datagrid把视图绑定,数据集难道删除一个就换一次啊?好象太废劲。

2006-04-25 20:04
快速回复:[求助]datagrid中数据 的删除问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012258 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved