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

[此贴子已经被作者于2006-4-21 15:44:24编辑过]

搜索更多相关主题的帖子: datagrid 数据 删除 
2006-04-21 15:42
yq_haha
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2006-4-22
收藏
得分:0 

你的是vb 好久没有用了 但是建议你 设计数据库的时候要有主外键 然后按照主外键查询 要实现删除后此条不显示的话,一般我用2种方法:1.删除后 重新刷新 2.DataGrid空间自带的删除功能


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



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

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