简体   繁体   中英

Datagridview rows & columns not clearing

I'm working on a project where I have to save the added row from Datagridview in database, after record saved I want to clear the Datagridview, here is the code I'm using to achieve it

Public Sub ClearGrid(ByRef gd As DataGridView)
    gd.Columns.Clear()
    gd.Rows.Clear()
    gd.DataSource = Nothing
End Sub

This code clears the rows & columns in Datagridview, but when I add another row and save again, the previously added record get saved again and newly added record also

I also tried to create a new instance of Datagridview myGrid = new Datagridview and then clear the rows & columns but the result was same

I'm fillig DataTable from Database & adding the rom to Datagridview using this code

 With MyDataTable.Rows(0)

     Dim row As String() = New String() _
                                 {.ItemArray(0).ToString(), .ItemArray(1).ToString(), _
                                 .ItemArray(1).ToString(), .ItemArray(3).ToString(), _
                                .ItemArray(4).ToString(), .ItemArray(5).ToString(), _
                                .ItemArray(6).ToString(), .ItemArray(7).ToString(), _
                                .ItemArray(8).ToString(), .ItemArray(9).ToString(), _
                                .ItemArray(10).ToString(), .ItemArray(11).ToString(), _
                                .ItemArray(12).ToString()}
     myGrid.Rows.Add(row)
 End With

You can't work the DataGridView both ways using simultaneously Rows and Columns and DataBinding (the DataSource) . You have to work either way.

From MSDN:

Data Display Modes in the Windows Forms DataGridView Control

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.

To answer your question

If you set the DataGridView.DataSource property to Nothing won't necessarily clear the row headers, because the data binding makes the DataGridView know the properties of the data source if you set its binding in the designer. So, even if you set it to nothing, it knows the columns that are required to be displayed.

If you really does want to clear the DataGridView completely, you will be required to remove its data binding from the designer and let the DataGridView build its columns collection dynamically through the data binding.

However, you will have to subscribe and handle the BindingContextChanged , or the BindingSource.ListChanged event in order to remove the undesired columns to display. It is prefered to always use a BindingSource so that the display columns are always set, and simply no rows display when no data is in, or your BindingSource.DataSource is set to Nothing , and this will keep displaying the column headers.

In addition, if you don't want the previsouly added objects to be saved along with the new ones that have been added after your DataGridView.DataSource = Nothing , then you have to remove them from the underlying list that was bound to your DataGridView, because all what this does is to undisplay the information, but, for example, if you bound to a DataTable instance, the rows you have added prior to DataSource = Nothing are still in there, so when you save, they're all saved along together.

Instead of setting your DataSource to Nothing , I recommend destroying the added rows from the underlying DataTable or whatever data source you may have.

As @Plutonix has already mentioned, your DataGridView is bound to the datasource and clearing the rows and columns isn't going to remove the source. This line:

gd.DataSource = Nothing

Should be all you need to clear grid, since this will unbind the source. Show the code for how you're saving new entries from the grid, because how you outline your process:

" but when I add another row and save again, the previously added record get saved again and newly added record also ",

describes the result you see in regards to how you have your code setup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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