none
删除选中的数据问题 RRS feed

  • 问题

  •  

        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

          

     

            For i As Integer = 0 To deletedtbase.RowCount - 1

                If (deletedtbase.Rows(i).Cells("Column1").Value) Then

                    Dim name As String = deletedtbase.Rows(i).Cells("Staff").Value

                    Dim objCon As New OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\taipan.accdb")

     

                    Dim objComm As New OleDbCommand()

     

                    objComm.CommandText = "DELETE FROM staff where Staff='" & (name) & "'"

                    MsgBox(objComm.CommandText)

                    objComm.Connection = objCon

     

                    Dim adapter As New OleDbDataAdapter(objComm)

                    objCon.Open()

                    objComm.ExecuteNonQuery()

                    Dim staffinfo As New DataTable

                    adapter.Fill(staffinfo)

                    deletedtbase.DataSource = staffinfo

     

                    objCon.Close()

                End If

            Next

                    pangkalan    重新绑定数据

        End Sub

     每次要删除2个以上的数据时,就会出现错误。

    Index was out of range. Must be non-negative and less than the size of the collection.

    Parameter name: index

    要怎样解决?

    2011年7月6日 7:45

答案

  • 我觉得应该先循环得到要删除的name,把数据全删除后再重新绑定
    http://feiyun0112.cnblogs.com/
    2011年7月6日 9:16
    版主
  •  

        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

          

     

            For i As Integer = 0 To deletedtbase.RowCount - 1

                If (deletedtbase.Rows(i).Cells("Column1").Value) Then

                    Dim name As String = deletedtbase.Rows(i).Cells("Staff").Value

                    Dim objCon As New OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\taipan.accdb")

     

                    Dim objComm As New OleDbCommand()

     

                    objComm.CommandText = "DELETE FROM staff where Staff='" & (name) & "'"

                    MsgBox(objComm.CommandText)

                    objComm.Connection = objCon

     

                    Dim adapter As New OleDbDataAdapter(objComm)

                    objCon.Open()

                    objComm.ExecuteNonQuery()

                    Dim staffinfo As New DataTable

                    adapter.Fill(staffinfo)

                    deletedtbase.DataSource = staffinfo

     

                    objCon.Close()

                End If

            Next

                    pangkalan    重新绑定数据

        End Sub

     每次要删除2个以上的数据时,就会出现错误。

     

    Index was out of range. Must be non-negative and less than the size of the collection.

    Parameter name: index

    要怎样解决?

     


    没有必要这样麻烦吧?你完全可以先把数据填充到DataTable中,然后借用自动生成的DeleteCommand,调用Update实现批量删除。

    比如:

    Using (OleDbDataAdapter adapter = new OleDbDataAdapter("select * from ……",new OleDbConnection(……))

         OleDbCommandBuilder obu = new OleDbCommandBuilder(adapter);

         Dim dt As new DataTable

         adapter.Fill(dt);

         '要删除某一行,直接使用——dt.Rows(索引号).Delete()

        ………………

        adapter.Update(dt);

        '重新绑定即可。

    End Using


    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    2011年7月7日 2:45
    版主

全部回复

  • 我觉得应该先循环得到要删除的name,把数据全删除后再重新绑定
    http://feiyun0112.cnblogs.com/
    2011年7月6日 9:16
    版主
  • 我觉得应该先循环得到要删除的name,把数据全删除后再重新绑定
    http://feiyun0112.cnblogs.com/
    可以给代码参考吗?
    2011年7月7日 1:57
  •  

        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

          

     

            For i As Integer = 0 To deletedtbase.RowCount - 1

                If (deletedtbase.Rows(i).Cells("Column1").Value) Then

                    Dim name As String = deletedtbase.Rows(i).Cells("Staff").Value

                    Dim objCon As New OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\taipan.accdb")

     

                    Dim objComm As New OleDbCommand()

     

                    objComm.CommandText = "DELETE FROM staff where Staff='" & (name) & "'"

                    MsgBox(objComm.CommandText)

                    objComm.Connection = objCon

     

                    Dim adapter As New OleDbDataAdapter(objComm)

                    objCon.Open()

                    objComm.ExecuteNonQuery()

                    Dim staffinfo As New DataTable

                    adapter.Fill(staffinfo)

                    deletedtbase.DataSource = staffinfo

     

                    objCon.Close()

                End If

            Next

                    pangkalan    重新绑定数据

        End Sub

     每次要删除2个以上的数据时,就会出现错误。

     

    Index was out of range. Must be non-negative and less than the size of the collection.

    Parameter name: index

    要怎样解决?

     


    没有必要这样麻烦吧?你完全可以先把数据填充到DataTable中,然后借用自动生成的DeleteCommand,调用Update实现批量删除。

    比如:

    Using (OleDbDataAdapter adapter = new OleDbDataAdapter("select * from ……",new OleDbConnection(……))

         OleDbCommandBuilder obu = new OleDbCommandBuilder(adapter);

         Dim dt As new DataTable

         adapter.Fill(dt);

         '要删除某一行,直接使用——dt.Rows(索引号).Delete()

        ………………

        adapter.Update(dt);

        '重新绑定即可。

    End Using


    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    2011年7月7日 2:45
    版主
  •  

        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

          

     

            For i As Integer = 0 To deletedtbase.RowCount - 1

                If (deletedtbase.Rows(i).Cells("Column1").Value) Then

                    Dim name As String = deletedtbase.Rows(i).Cells("Staff").Value

                    Dim objCon As New OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\taipan.accdb")

     

                    Dim objComm As New OleDbCommand()

     

                    objComm.CommandText = "DELETE FROM staff where Staff='" & (name) & "'"

                    MsgBox(objComm.CommandText)

                    objComm.Connection = objCon

     

                    Dim adapter As New OleDbDataAdapter(objComm)

                    objCon.Open()

                    objComm.ExecuteNonQuery()

                    Dim staffinfo As New DataTable

                    adapter.Fill(staffinfo)

                    deletedtbase.DataSource = staffinfo

     

                    objCon.Close()

                End If

            Next

                    pangkalan    重新绑定数据

        End Sub

     每次要删除2个以上的数据时,就会出现错误。

     

    Index was out of range. Must be non-negative and less than the size of the collection.

    Parameter name: index

    要怎样解决?

     


    没有必要这样麻烦吧?你完全可以先把数据填充到DataTable中,然后借用自动生成的DeleteCommand,调用Update实现批量删除。

    比如:

    Using (OleDbDataAdapter adapter = new OleDbDataAdapter("select * from ……",new OleDbConnection(……))

         OleDbCommandBuilder obu = new OleDbCommandBuilder(adapter);

         Dim dt As new DataTable

         adapter.Fill(dt);

         '要删除某一行,直接使用——dt.Rows(索引号).Delete()

        ………………

        adapter.Update(dt);

        '重新绑定即可。

    End Using


    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处

     

            For i As Integer = 0 To deletedtbase.RowCount - 1

                If (deletedtbase.Rows(i).Cells("Column1").Value) Then

     

                    '   Dim ID As Integer = deletedtbase.Rows(i).Cells("ID").Value

                    Dim objCon As New OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\taipan.accdb")

     

                    Dim objComm As New OleDbCommand()

     

                    objComm.CommandText = "select * from staff"

                    ' MsgBox(objComm.CommandText)

                    objComm.Connection = objCon

     

                    Dim adapter As New OleDbDataAdapter(objComm)

                    objCon.Open()

                    'objComm.ExecuteNonQuery()

                    Dim staffinfo As New DataTable

                    adapter.Fill(staffinfo)

                    staffinfo.Rows(i).Delete()

     

     

                    adapter.Update(staffinfo)

                    deletedtbase.DataSource = staffinfo

     

                    objCon.Close()

     

                End If

            Next

            pangkalan()

     出现错误Update requires a valid DeleteCommand when passed DataRow collection with deleted rows.我已经设定stafnamef为primary key,也不行。


    2011年7月7日 4:15
  • 楼主你好:)

    错误原因是因为你没有为adapter生成对应的DeleteCommand,建议安装我的方法,使用OleDbCommandBuilder来做。


    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    2011年7月7日 4:58
    版主
  • 楼主你好:)

    错误原因是因为你没有为adapter生成对应的DeleteCommand,建议安装我的方法,使用OleDbCommandBuilder来做。


    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处

     

       For i As Integer = 0 To deletedtbase.RowCount - 1

                If (deletedtbase.Rows(i).Cells("Column1").Value) Then

     

                    '   Dim ID As Integer = deletedtbase.Rows(i).Cells("ID").Value

                    Dim objCon As New OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" &Application.StartupPath & "\taipan.accdb")

     

                    Dim objComm As New OleDbCommand()

     

                    objComm.CommandText = "select * from staff"

                    ' MsgBox(objComm.CommandText)

                    objComm.Connection = objCon

     

                    Dim adapter As New OleDbDataAdapter(objComm)

                    objCon.Open()

     

      Dim CommandBuilder1 As New OleDbCommandBuilder(adapter) 

                   

                    Dim staffinfo As New DataTable 

                    adapter.Fill(staffinfo)

                    staffinfo.Rows(i).Delete()

      adapter.Update(staffinfo)

                    deletedtbase.DataSource = staffinfo

     

                    objCon.Close()

     

                End If

            Next

            pangkalan()

     

     

    出现错误。我的数据库有8个数据。假设我选择4,5的checkbox,第4行的数据会删除但第五行的数据不会删除,而且会出现以下的错误?

    Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index


     

     

    2011年7月7日 7:03
  • 你好:)

    麻烦做这样的改正:

    Dim objCon As New OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" &Application.StartupPath & "\taipan.accdb")

    Dim objComm As New OleDbCommand()

    objComm.CommandText = "select * from staff"

    ' MsgBox(objComm.CommandText)

    objComm.Connection = objCon

    Dim adapter As New OleDbDataAdapter(objComm)

    objCon.Open()

    Dim CommandBuilder1 As New OleDbCommandBuilder(adapter)

    Dim staffinfo As New DataTable

    adapter.Fill(staffinfo)

    For i As Integer = 0 To deletedtbase.RowCount - 1

    If (deletedtbase.Rows(i).Cells("Column1").Value) Then

    staffinfo.Rows(i).Delete()

                   

    i=i-1   '增加这句话

    End If

    Next

    adapter.Update(staffinfo)
    deletedtbase.DataSource = staffinfo
    objCon.Close()


    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    2011年7月7日 7:23
    版主
  • 你好:)

    麻烦做这样的改正:

    Dim objCon As New OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" &Application.StartupPath & "\taipan.accdb")

    Dim objComm As New OleDbCommand()

    objComm.CommandText = "select * from staff"

    ' MsgBox(objComm.CommandText)

    objComm.Connection = objCon

    Dim adapter As New OleDbDataAdapter(objComm)

    objCon.Open()

    Dim CommandBuilder1 As New OleDbCommandBuilder(adapter)

    Dim staffinfo As New DataTable

    adapter.Fill(staffinfo)

    For i As Integer = 0 To deletedtbase.RowCount - 1

    If (deletedtbase.Rows(i).Cells("Column1").Value) Then

    staffinfo.Rows(i).Delete()

                   

    i=i-1   '增加这句话

    End If

    Next

    adapter.Update(staffinfo)
    deletedtbase.DataSource = staffinfo
    objCon.Close()


    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    你好,上面的代码运行后没有错误,但被选中的数据还是没有被删除。
    2011年7月7日 7:46
  • 你好:)

    估计是没有进入If判断循环,或者是If条件都不满足。

    请问——deletedtbase.Rows(i).Cells("Column1").Value是什么东西?


    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    2011年7月7日 7:49
    版主
  • 你好:)

    估计是没有进入If判断循环,或者是If条件都不满足。

    请问——deletedtbase.Rows(i).Cells("Column1").Value是什么东西?


    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    deletedtbase.Rows(i).Cells("Column1").Value是在deletedbase(datagridview)的checkbox.value
    2011年7月7日 7:53
  • deletedtbase.Rows(i).Cells("Column1").Value是在deletedbase(datagridview)的checkbox.value

    建议你使用类似:

    deletedtbase.Rows(i).Cells("Column1").Value==true或者类似代码。顺便把调试结果告诉我——有没有进入到If里边执行Delete命令?


    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    2011年7月7日 7:55
    版主
  • deletedtbase.Rows(i).Cells("Column1").Value是在deletedbase(datagridview)的checkbox.value

    建议你使用类似:

    deletedtbase.Rows(i).Cells("Column1").Value==true或者类似代码。顺便把调试结果告诉我——有没有进入到If里边执行Delete命令?


    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
     For i As Integer = 0 To deletedtbase.RowCount - 1
                If (deletedtbase.Rows(i).Cells("Column1").Value = True) Then
                    MsgBox(i)
                    staffinfo.Rows(i).Delete()
                    i = i - 1   '增加这句话
                End If
            Next
    我增加msgbox(i)这段代码,假设我选择是第4行,msgbox就会一直显示4,就算我按确定。
    2011年7月7日 8:07
  • 你好:)

    你确定只选择了这一行?第四行一下的都没有选择?

    另外你的deletedtbase也是一个DataTable,还是一个GridView?


    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    2011年7月7日 8:16
    版主
  • 你好:)

    你确定只选择了这一行?第四行一下的都没有选择?


    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    是的,不管我选多少行都一样
    2011年7月7日 8:18
  • 另外你的deletedtbase也是一个DataTable,还是一个GridView?
    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    2011年7月7日 8:19
    版主
  • 另外你的deletedtbase也是一个DataTable,还是一个GridView?
    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    deletedtbase是一个datagridview
    2011年7月7日 8:25
  • deletedtbase是一个datagridview

    你把staffinfo绑定到deletedbase上的?


    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    2011年7月7日 8:37
    版主
  • deletedtbase是一个datagridview

    你把staffinfo绑定到deletedbase上的?


    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    是的。
    2011年7月7日 8:46
  • 楼主,这样吧:)

    考虑你DataGridView和内存中的DataTable不同步导致这个问题的产生——我建议你在DataTable中增加一字段(是否选中)。然后逐行循环判断,删除即可。


    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处


    2011年7月7日 9:06
    版主
  • 楼主,这样吧:)

    考虑你DataGridView和内存中的DataTable不同步导致这个问题的产生——我建议你在DataTable中增加一字段(是否选中)。然后逐行循环判断,删除即可。


    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处


    不好意思,我不太明白。可否给代码参考?
    2011年7月7日 9:34
  • 不好意思,我不太明白。可否给代码参考?
    就是你用那个绑定的DataTable,使用staffinfo.Columns.Add("Isselected",typeof(bool)),然后绑定到GridView上,然后循环判断Isselected是否是true进行删除。
    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    2011年7月8日 0:56
    版主