vb.net 如何删除和清除 vb 2010 中的 datagridview 行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34207054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete and clear the datagridview rows in vb 2010
提问by Iftikhar uddin
I am developing a Point of Sale System in VB and using access as RDBMS. Everything went well however when it came to deletion i got stuck. I want to clear all the rows from the datagridview and want to delete it from database also.The new datagridview will store new items and i am sending that new data to print.I tried the below codes but all in vain.
我正在 VB 中开发一个销售点系统,并使用访问作为 RDBMS。一切都很顺利,但是在删除时我被卡住了。我想清除 datagridview 中的所有行,也想从数据库中删除它。新的 datagridview 将存储新项目,我正在发送新数据进行打印。我尝试了下面的代码,但都是徒劳的。
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
For i As Integer = 0 To SProductDataGridView.RowCount
SProductDataGridView.Rows.Remove(SProductDataGridView.Rows(0))
Next
End Sub
2nd Try
第二次尝试
For i As Integer = 0 To SProductDataGridView.RowCount -1
ProductTableAdapter.DeleteProduct(IDTextBox.Text)
ProductTableAdapter.Fill(MyDS.product)
ProductTableAdapter.Dispose()
Next
Note :gridview.rows.clear() + gridview.ds= nothing , i tried that butgridview.rows.clear() doesn't work and gridview.ds = nothing just clear the gridview and is not deleting data from access database.
注意:gridview.rows.clear() + gridview.ds= nothing ,我试过 butgridview.rows.clear() 不起作用并且 gridview.ds = nothing 只是清除 gridview 并且不会从访问数据库中删除数据。
回答by Reza Aghaei
You can use either of below options:
您可以使用以下任一选项:
- Using DataTable
- Using DataGridView
- Using BindingSource
- 使用数据表
- 使用 DataGridView
- 使用绑定源
The logic of all solutions is removing the rows and then updating the table adapter.
所有解决方案的逻辑是删除行然后更新表适配器。
Using DataTable
使用数据表
You can find each rows in the data table and delete it, then update the table adapter:
您可以找到数据表中的每一行并将其删除,然后更新表适配器:
For Each row As DataRow In Me.TestDBDataSet.TestTable.Rows
row.Delete()
Next
Me.TestTableTableAdapter.Update(TestDBDataSet.TestTable)
Using DataGridView
使用 DataGridView
For Each row As DataGridViewRow In Me.TestTableDataGridView.Rows
DirectCast(row.DataBoundItem, DataRowView).Delete()
Next
Me.TestTableTableAdapter.Update(TestDBDataSet.TestTable)
Using BindingSource
使用绑定源
For Each item As Object In TestTableBindingSource
TestTableBindingSource.Remove(item)
Next
Me.TestTableTableAdapter.Update(TestDBDataSet.TestTable)

