How to delete all rows except the Header from the datatable?

Hi All,
How to delete all rows except the Header from the data table?

PatientName TIMESTAMP Account Number Facility Payer
ABC 1 123 x Z
CDC 1 321 x Z
ZZZ 1 456 x Z
kkk 1 987 x Z
MMM 1 897 x Z

I want to keep only the header in

Thanks in advance.
Andy

@Andy5
if clearing means keep structure and remove rows - then:
yourDT = yourDT.Clone

if clearing means reset it to null
yourDT = nothing

if clearing means reset it to initialized form, that it can bused for further work
yourDT = new DataTable()

Post updated.

@Andy5

Use Clear Data Table activity and pass Datatable variable in it.

Thanks

1 Like

@Andy5
You want to keep only headers then
Use assign activity.
Left side
ā†’ YourDT
Right side
ā†’ YourDT.Clone

i need to keep only Header if i used ā€œUse assign activity.
Left side
ā†’ YourDT
Right side
ā†’ YourDT.Cloneā€ then it will clone all data?

Thanks in advance.
Andy

1 Like

it will clear all data with Headersā€¦

Thanks in advance.
Andy

1 Like

@Andy5
Clone operation only cloning header

Thanks will try

@Andy5
Sureā€¦and donā€™t forget to mark solution if your problem solve.

Sure, I will

1 Like

Hi @Andy5

You can simply retain the header like this:

ā†’ Read the excel with with ā€˜Add Headersā€™ option disabled.

ā†’ Use this expression in the Assign activity:

yourDt = yourDt.AsEnumerable.Take(1).CopyToDataTable

Now yourDt will only contain the header details.

Hope this helps,
Best Regards.

Hi @Andy5 ,

you can use clear data table activity to delete all the rows except the headers from the data table.

Cheers

@Andy5

It will not clear the headersā€¦It will only clear the rows data.

Thanks,
Pratik

Hi @Andy5

Read the excel by using read range activity and store the data in a variable.
Pass the variable into clear data table activity and run the code so that all the rows get deleted from the data table except the header.

Hope it helps !!
Regards

Hi @Andy5 ,

If you wish to delete all rows except header use the activity clear datatable .

Please Refer the image below

1 Like

Hello this is Gulshan Negi
Well, to delete all rows except the Header from the datatable.

dataTable.Rows.Cast().Where(row => row != dataTable.Rows[0]).ToList().ForEach(row => row.Delete());
dataTable.AcceptChanges();

I hope you are clear now.
Thanks

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.