关于vb.net:Datagridview行

Datagridview rows & columns not clearing

我正在一个项目中,我必须将Datagridview中添加的行保存到数据库中,保存记录后,我想清除Datagridview,这是我用来实现它的代码

1
2
3
4
5
Public Sub ClearGrid(ByRef gd As DataGridView)
    gd.Columns.Clear()
    gd.Rows.Clear()
    gd.DataSource = Nothing
End Sub

此代码清除行


不能同时使用行和列以及数据绑定(数据源)来同时使用DataGridView。您必须以任何一种方式进行工作。

来自MSDN:

Windows窗体DataGridView控件中的数据显示模式

Unbound

Unbound mode is suitable for displaying relatively small amounts of data that you manage programmatically. You do not attach the DataGridView control directly to a data source as in bound mode. Instead, you must populate the control yourself, typically by using the DataGridViewRowCollection.Add method.

Unbound mode can be particularly useful for static, read-only data, or when you want to provide your own code that interacts with an external data store. When you want your users to interact with an external data source, however, you will typically use bound mode.

For an example that uses a read-only unbound DataGridView, see How to: Create an Unbound Windows Forms DataGridView Control.

Bound

Bound mode is suitable for managing data using automatic interaction with the data store. You can attach the DataGridView control directly to its data source by setting the DataSource property. When the control is data bound, data rows are pushed and pulled without the need of explicit management on your part. When the AutoGenerateColumns property is true, each column in your data source will cause a corresponding column to be created in the control. If you prefer to create your own columns, you can set this property to false and use the DataPropertyName property to bind each column when you configure it. This is useful when you want to use a column type other than the types that are generated by default. For more information, see Column Types in the Windows Forms DataGridView Control.
For an example that uses a bound DataGridView control, see Walkthrough: Validating Data in the Windows Forms DataGridView Control.

You can also add unbound columns to a DataGridView control in bound mode. This is useful when you want to display a column of buttons or links that enable users to perform actions on specific rows. It is also useful to display columns with values calculated from bound columns. You can populate the cell values for calculated columns in a handler for the CellFormatting event. If you are using a DataSet or DataTable as the data source, however, you might want to use the DataColumn.Expression property to create a calculated column instead. In this case, the DataGridView control will treat calculated column just like any other column in the data source.

Sorting by unbound columns in bound mode is not supported. If you create an unbound column in bound mode that contains user-editable values, you must implement virtual mode to maintain these values when the control is sorted by a bound column.

回答您的问题

如果将DataGridView.DataSource属性设置为Nothing,则不一定会清除行标题,因为如果在设计器中设置了数据绑定,则数据绑定会使DataGridView知道数据源的属性。因此,即使您将其设置为空,它也知道需要显示的列。

如果您确实确实想完全清除DataGridView,则将需要从设计器中删除其数据绑定,并让DataGridView通过数据绑定动态构建其列集合。

但是,您必须订阅并处理BindingContextChanged或BindingSource.ListChanged事件,以便删除要显示的不需要的列。最好始终使用BindingSource以便始终设置显示列,并且当没有数据输入或者BindingSource.DataSource设置为Nothing时根本不显示行,这将继续显示列标题。

此外,如果您不希望将预先添加的对象与在DataGridView.DataSource = Nothing之后添加的新对象一起保存,则必须将其从绑定到您的基础列表中删除DataGridView,因为这样做是为了不显示信息,但是例如,如果您绑定到DataTable实例,则在DataSource = Nothing之前添加的行仍在其中,因此在保存时,它们全部一起保存。

建议不要从基础DataTable或任何可能的数据源中破坏添加的行,而不是将DataSource设置为Nothing


正如@Plutonix已经提到的那样,您的DataGridView已绑定到数据源,并且清除行和列不会删除源。
这行:

gd.DataSource = Nothing

应该是清除网格的全部,因为这将取消绑定源。
显示有关如何从网格中保存新条目的代码,因为您如何概述过程:

",但是当我添加另一行并再次保存时,先前添加的记录将再次保存,新添加的记录也会被保存",

描述有关代码设置方式的结果。