首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

gridview有关问题,查询自己写,显示在gridview里,还要编辑和删除

2012-01-15 
gridview问题,查询自己写,显示在gridview里,还要编辑和删除.我的思路是,查询自己写,然后显示在gridview里,

gridview问题,查询自己写,显示在gridview里,还要编辑和删除.
我的思路是,查询自己写,然后显示在gridview里,然后利用gridview里的事件对查询后的结果进行编辑和删除,现在贴上代码,大家帮看看有什么问题.
查询:
Protected   Sub   Button1_Click(ByVal   sender   As   Object,   ByVal   e   As   System.EventArgs)   Handles   Button1.Click
                Dim   myConnectionString   As   String
                myConnectionString   =   "server=localhost;database=****;uid=****;pwd=123; "
                Dim   myConnection   As   New   SqlConnection(myConnectionString)
                Dim   mySelect   As   String   =   "SELECT   *   FROM   Repository   WHERE   Name   LIKE   '% "   +   TextBox3.Text   +   "% '   AND   Operator   LIKE   '% "   +   DropDownList1.Text   +   "% '   AND   Type   LIKE   '% "   +   DropDownList2.Text   +   "% '   AND   Server   LIKE   '% "   +   DropDownList3.Text   +   "% '   AND   Detail   LIKE   '% "   +   TextBox4.Text   +   "% '   AND   Keyword   LIKE   '% "   +   TextBox6.Text   +   "% ' "
                Dim   myCommand   As   New   SqlCommand(mySelect)
                Dim   adapter   As   SqlDataAdapter   =   New   SqlDataAdapter(mySelect,   myConnection)
                myCommand.Connection   =   myConnection
                myConnection.Open()
                Dim   ds   As   New   DataSet
                adapter.Fill(ds)
                GridView1.DataSource   =   ds
                GridView1.DataBind()
                myCommand.Connection.Close()
        End   Sub
删除:
  Protected   Sub   GridView1_RowDeleting(ByVal   sender   As   Object,   ByVal   e   As   System.Web.UI.WebControls.GridViewDeleteEventArgs)   Handles   GridView1.RowDeleting
                Dim   myConnectionString   As   String
                myConnectionString   =   "server=localhost;database=****;uid=****;pwd=123; "
                Dim   myConnection   As   New   SqlConnection(myConnectionString)
                Dim   myDelete   As   String
              myDelete   =   "DELETE   FROM   Repository   WHERE   RepositoryID   = 'GridView1.DataKeyNames[e.RowIndex][0]) ' "
                Dim   myCommand   As   New   SqlCommand(myDelete)
                myCommand.Connection   =   myConnection


                myConnection.Open()
                myCommand.ExecuteNonQuery()
                myConnection.Close()
        End   Sub
运行结果:
运行无异常,但是结果没有删除,   myDelete   =   "DELETE   FROM   Repository   WHERE   RepositoryID   = 'GridView1.DataKeyNames[e.RowIndex][0]) ' "主要是这个语句,大家帮我看看有没有问题.我是在查询的结果里面做删除.

[解决办法]
myDelete = "DELETE FROM Repository WHERE RepositoryID = 'GridView1.DataKeyNames[e.RowIndex][0]) ' "
????难道RepositoryID是“GridView1.DataKeyNames[e.RowIndex][0])”

应该是这样吧
myDelete = "DELETE FROM Repository WHERE RepositoryID = ' " & GridView1.DataKeyNames(e.RowIndex)(0) & " ' "

热点排行