简体   繁体   English

清除绑定到DataSet的DataGridView而不丢失标题VB.NET

[英]Clearing DataGridView bound to DataSet without losing headers VB.NET

I've currently got an application that you can search for a specific location, owner, etc. which will return some basic information about the device(s). 我目前有一个可以搜索特定位置,所有者等的应用程序,它将返回有关设备的一些基本信息。 I have this information pulled via a SQL query and placed into a DataSet which is then placed into a DataGridView object. 我通过SQL查询提取了此信息,并将其放入DataSet中,然后将其放入DataGridView对象中。 I am trying to implement a Clear button which will remove the search terms as well as the data in the DataGridView object. 我正在尝试实现一个清除按钮,该按钮将删除搜索词以及DataGridView对象中的数据。

Unfortunately, I cannot use DataGridView.Rows.Clear() as my DataGridView is bound to a DataSet. 不幸的是,我无法使用DataGridView.Rows.Clear()因为我的DataGridView已绑定到DataSet。 I am running into difficulty using DataGridView.DataSource = Nothing as this is not only removing the data that was displayed, but also removes my headers for columns that I have created for the DataGridView. 我在使用DataGridView.DataSource = Nothing时遇到了麻烦,因为这不仅会删除显示的数据,还会删除我为DataGridView创建的列的标题。 I have tried a few solutions I've found such as DataGridView.Refresh() and DataGridView.AutoGenerateColumns = false but these have thus far removed the column headers. 我已经尝试了一些找到的解决方案,例如DataGridView.Refresh()DataGridView.AutoGenerateColumns = false但是到目前为止,这些解决方案已经删除了列标题。

This is the code I am using for adding rows to the DataGridView object; 这是我用来向DataGridView对象添加行的代码。

    Dim con As New SqlClient.SqlConnection(getConnectionString)
        Dim cmd As New SqlCommand("SELECT asset.assetID, owner.ownerName, model.brand, asset.modelID, deviceOwner.serialNumber, deviceOwner.location, asset.dateAssessed from asset 
                                  join deviceOwner on asset.assetID = deviceOwner.assetID
                                  join owner on deviceOwner.ownerID = owner.OwnerID
                                  join model on asset.modelID = model.modelID where asset.dateAssessed = @checkDate", con)
        cmd.Parameters.AddWithValue("@checkDate", searchEnterDate.Text.ToString)
        con.Open()
        Dim myDA As New SqlDataAdapter(cmd)
        myDA.Fill(myDataSet, "MyTable")
        displayResults.DataSource = myDataSet.Tables("MyTable").DefaultView
        con.Close()
        con = Nothing

I am looking for a solution that will allow me to clear the rows from the DataGridView without removing the column headers so I can use the clear button if necessary but continue to search with column headers still displaying. 我正在寻找一种解决方案,该解决方案将允许我从DataGridView清除行而不删除列标题,因此我可以在必要时使用清除按钮,但继续搜索仍显示列标题。

You could Clone the DataTable which keeps columns but "removes" data: 你可以CloneDataTable这使列,但“删除”的数据:

Dim oldTable As DataTable = CType(displayResults.DataSource, DataView).Table
Dim emptyTable As DataTable = oldTable.Clone()
displayResults.DataSource = emptyTable ' or emptyTable.DefaultView

or use DataRowCollection.Clear on the old table which could cause more side-effects: 或在旧表上使用DataRowCollection.Clear ,这可能会导致更多副作用:

oldTable.Rows.Clear() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM