VB.NET:清除 DataGridView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2217175/
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
VB.NET: Clear DataGridView
提问by Bibhas Debnath
I've tried -
我试过了 -
DataGridView1.DataSource=Nothing
and
和
DataGridView1.DataSource=Nothing
DataGridView1.Refresh()
and
和
DataGridView1.RefreshEdit()
None of them works..
他们都没有工作..
I've written a method that sets the DataSource of the DataGridView when executed. but each time i execute it, it replicates the data with new value and appends it to the previous contents of the DGV.. I wanna clear the content and then add the values.. Is that possible?
我写了一个方法,在执行时设置 DataGridView 的数据源。但是每次我执行它时,它都会用新值复制数据并将其附加到 DGV 的先前内容中.. 我想清除内容然后添加值.. 这可能吗?
回答by Alex Essilfie
If the DataGridView is bound to any datasource, you'll have to set the DataGridView's DataSource
property to Nothing
.
如果 DataGridView 绑定到任何数据源,则必须将 DataGridView 的DataSource
属性设置为Nothing
。
If the DataGridView is not bound to any data source, this code will do the trick:
如果 DataGridView 未绑定到任何数据源,则此代码将起作用:
DataGridView.Rows.Clear()
回答by rheitzman
For unbound cases note that:
对于未绑定的情况,请注意:
DataGridView.Rows.Clear()
leaves the Columns collection in place.
保留 Columns 集合。
DataGridView.Columns.Clear()
..will remove all the columns and rows. If you are using the DGV unbound, and on next use the columns change, clearing the Rows may not be adequate. For library code clear all the columns before adding columns.
..将删除所有列和行。如果您使用未绑定的 DGV,并且在下次使用时列更改,则清除 Rows 可能是不够的。对于库代码,在添加列之前清除所有列。
回答by Jason Punyon
I'd probably use this...
我可能会用这个...
DataGridView1.Rows.Clear()
to clear out the rows and then rebind.
清除行然后重新绑定。
回答by Nal MEN
Follow the easy way like this
按照这样的简单方法
assume that ta
is a DataTable
假设这ta
是一个DataTable
ta.clear()
DataGridView1.DataSource = ta
DataGridView1.DataSource = Nothing
回答by user3717731
Don't do anything on DataGridView
, just clear the data source. I tried clearing myDataset.clear()
method, then it worked.
不要对 做任何事情DataGridView
,只需清除数据源。我尝试了清除myDataset.clear()
方法,然后它起作用了。
回答by FiveTools
Can you not bind the datagridview to an empty collection (instead of null). That do the trick?
你不能将 datagridview 绑定到一个空集合(而不是 null)。那行得通吗?
回答by karthikeyan
To remove the old record in datagridview when you are searching for new result,with button_click event write the following code,
要在搜索新结果时删除 datagridview 中的旧记录,请使用 button_click 事件编写以下代码,
me.DataGridview1.DataSource.clear()
me.DataGridview1.DataSource.clear()
this code will help to remove the old record in datagridview.
此代码将有助于删除 datagridview 中的旧记录。
回答by Ron Rebennack
I found that setting the datasource to null removes the columns. This is what works for me:
我发现将数据源设置为 null 会删除列。这对我有用:
c#:
C#:
((DataTable)myDataGrid.DataSource).Rows.Clear();
VB:
VB:
Call CType(myDataGrid.DataSource, DataTable).Rows.Clear()
回答by leoraelkins
My DataGridView is also bound to a DataSource and myDataGridView.Columns.Clear()
worked fine but myDataGridView.Rows.Clear()
did NOT. Just an FYI for those who have tried .Rows
.
我的 DataGridView 也绑定到 DataSource 并且myDataGridView.Columns.Clear()
工作正常但myDataGridView.Rows.Clear()
没有。仅供尝试过的人参考.Rows
。
回答by Micha? R
Dim DS As New DataSet
DS.Clear()
- DATASET clear works better than DataGridView.Rows.Clear()
for me :
DS.Clear()
- DATASET clear 比DataGridView.Rows.Clear()
我好用:
Public Sub doQuery(sql As String)
Try
DS.Clear() '<-- here
' - CONNECT -
DBCon.Open()
' Cmd gets SQL Query
Cmd = New OleDbCommand(sql, DBCon)
DA = New OleDbDataAdapter(Cmd)
DA.Fill(DS)
' - DISCONNECT -
DBCon.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub