Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make datatable blank in Vb.net?

I have a data table which has few rows and columns. I need to delete the entire data along with column names also.

Eg :

                 Item | 2017 | 2018 | 2019 | 2020
                ----------------------------------
                 A      400    200   300     500
                 B      400    300   500     100
                 C      100    200   500     300
                 D      100    300   400     100

I need to delete all the data of the above data table along with the column names so that I can re-use the same data table for storing other values.

I tried datatable.clear() method it is clearing only the data, the column names remain intact and also I can't use dispose().

How can I do it.?

like image 537
Kumar Avatar asked Dec 07 '25 06:12

Kumar


2 Answers

This should work:

datatable.Clear()             'clear records
datatable.Constraints.Clear() 'clear constraints (required before Columns.Clear(), if there is any constraint)
datatable.Columns.Clear()     'clear columns

Or this:

datatable.Reset()

But if you want to just create a new "blank" instance, or in other words, reinitialize it:

datatable = New Datatable
like image 164
MatSnow Avatar answered Dec 10 '25 02:12

MatSnow


Short version here:

Dim dt As New DataTable

'...
'you do some operations
'...

dt = New DataTable 'renew the same instance

Alternatively, you can use:

dt = Nothing 'basically setting it to NULL
like image 30
boop_the_snoot Avatar answered Dec 10 '25 01:12

boop_the_snoot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!