我在从vb.net的datagridview中删除一行时遇到问题 [英] i have a problem in deleting a row from datagridview in vb.net

查看:128
本文介绍了我在从vb.net的datagridview中删除一行时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我在数据库中有一个表,并且在表单中有一个datagridview.gridview绑定到表上.我的表单上还有一个按钮,其名称为Delete.当我的网格视图显示表数据时,然后在网格视图中选择一行.然后单击按钮.我选择的行应从gridview以及db表中删除.我的代码在此处用于此工作:

Hello All,
I have a table in database.and a datagridview in my form.gridview bindes to the table.also my form has a button that its name is Delete.when my grid view shows the tables data and then i choose a row in grid view.and then click the button.the row which i choose should be deleted from gridview and also from table in db.my code is here for this job:

<pre lang="vb">Private Sub ButtonX4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX4.Click<br />
        For Each rows As DataGridViewRow In DataGridView2.SelectedRows<br />
            DataGridView2.Rows.Remove(rows)<br />
            Dim command As SqlCommand<br />
            Dim delete As String<br />
            delete = "DELETE FROM [leitnertable] Where (ID) = " & DataGridView2.CurrentRow.Cells(0).Value & ""<br />
            command = New SqlCommand(delete, con)<br />
            If command.Connection.State = ConnectionState.Closed Then command.Connection.Open()<br />
            command.ExecuteNonQuery()<br />
        Next<br />
    End Sub</pre><br />


ID是我的表格列之一.
问题是此代码从网格视图中删除了我选择的行,但是此代码并未从db中的表中删除我选择的行.此代码从db中的表中删除了我选择的行的下一行.
我需要快速帮助.

感谢All in Advance.


ID is one of my table columns.
The problem is that this code delete the row which i choose, from grid view but this code dose not delete the row i choose, from table in my db.ths code delete the next row of the row i choose,from table in db.
I need a quick help.

Thanks to All in Advance.

推荐答案

您好,
请点击以下链接.可能会对您有帮助.
http://www.eggheadcafe.com/community/aspnet/2/10045544/help-to-delete-rows-in-gridview.aspx [
Hello,
Please follow the below link. it may help you.
http://www.eggheadcafe.com/community/aspnet/2/10045544/help-to-delete-rows-in-gridview.aspx[^]

thanks
sanjeev


根据我的情况
将您的代码放置在DataGridView中,以加载/获取数据
函数中可以命名吗
我喜欢这样
宣告功能
As per me
Place your Code what you have Written to load/Fetch the Data in DataGridView
inside a Function May you name it
I Did like this
Declared a Function
Public Sub RecordDelete()
        Dim id As Integer = DataGridView1.SelectedCells.Item(0).Value()
        Dim cmd As New SqlCommand("Delete From Tst_Bank where ID=''" & id & "''", myCon.con())
        cmd.ExecuteNonQuery()
        loadData()
    End Sub


然后在删除"按钮上单击


And on Delete Button Click

RecordDelete()


在loadData()函数下,我正在DataGridView中获取数据.
无需使用
DataGridView2.Rows.Remove(行)
根据您的要求实现此目的


Under loadData() Function I am Getting the Data inside the DataGridView.
No need to Use
DataGridView2.Rows.Remove(rows)
Implement this as per your Requirement


Private Sub ButtonX4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX4.Click
        For Each rows As DataGridViewRow In DataGridView2.SelectedRows
            
            Dim command As SqlCommand
            Dim delete As String
            delete = "DELETE FROM [leitnertable] Where (ID) = " & DataGridView2.CurrentRow.Cells(0).Value & ""
            command = New SqlCommand(delete, con)
            If command.Connection.State = ConnectionState.Closed Then command.Connection.Open()
            command.ExecuteNonQuery()
            command.Connection.Close()
'Just first remove from the database and then reload the datagrid or remove it manually... as you like
            DataGridView2.Rows.Remove(rows)
        Next
    End Sub



希望有帮助:)



hope this help :)


这篇关于我在从vb.net的datagridview中删除一行时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆