How to count rows in DataTable?


I have problem with getting a count of the number of rows in a data table, the Data Table is created from a Data Scraping.

Even if the Data Table is empty (I checked it for sure by export to csv file), DataTable.Rows.Count.ToString shows 1 row.
Also if Data Table has one row, counter still shows 1.

I tried with indexes, DataTable.Rows.IndexOf(row).ToString, with empty list Index = 0, with one row Index = 0, with two rows there are three indexes 0, 1 and 2.

Is there another way to count DataTable rows ? (I would like to avoid the solution of writing to csv file and then counting number of rows from it :slight_smile: )


create a variable counter
initialize the counter in 0 before the for each
inside the for each increment counter with assign activity counter = counter+1

1 Like

Even if the DataTable has no values and returns nothing to csv file, the for each loop starts. I put DataTable.Rows.IndexOf(row).ToString inside For each and with empty list I’ve got index =0 and with one row list index also equals 0. For number of rows greater than 1 indexes are OK, from 0 to DataTable.Rows.Count for example two rows: 0,1,2. Three rows: 0,1,2,3 etc. I’ve got problem only with identify if there is one or none rows in DataTable.Thanks for the answer but It will not works.

What does OutputDataTable say?

OutputDataTable returns something, if I put OutputDataTable result into string and MessageBox, I see nothing but the len(string) = 4. String.IsNullOrEmpty(string) and IsNothing(string) does not work. Now I can check the len statement of OutputDataTable, of course if it will be always working ? Thanks :slight_smile:

Output data table returns following:
If data table empty (returns a column(as default header) and count 1)
If data table has one row (returns first item with count 1)

Hi Mac,

Have you tried deleting empty/Blank datarows and then trying to count the number of rows? After the removal of empty rows, you should get the row count correct.


Hi @Amrita_Mogali

The above points made by reading empty sheet.(used Read range activity).

Hi Amrita, how to identify and delete empty/Blank rows ?

In conclusion with empty list:

  • DataTable.Rows.Count = 1
  • OutputDataTable returns len(string) = 4
  • String.IsNullOrEmpty(string) and IsNothing(string) does not work
  • In For each row loop, loop starts but row.Item(0).ToString = IndexOutOfRange Error, the same error with row.Item(1).ToString
  • export DataTable to csv gives empty file (working with list of one row, DataTable.Rows.Count = 1 and csv have two rows, one with column names and datarow)


use this: datableName.rows.count


Hello! I am having this same issue. Did you ever find a solution?


Thanks anyway, but I have found that the Filter Data Table activity works!

I have successfully tested in where my dt should be blank and was returning a count of 1 (it now returns 0, as expected); I also tested it where I had rows with data separated by blank rows.

1 Like

cheers !

Hello Mac, could you try shit solution ?

  1. Create new Datatable object before you scrape data DT = New System.Data.DataTable
  2. Create breakpoint after scraping data activity
  3. Run solution in debbuging
  4. When process will stop at breakpoint please search for datatable variable in locals, expand it and create screen shot of the content
  5. Publish screen shot with content

Probably your rows.count = 1 is connected with headers but i have to check it.

@pattyricarte The problem is that rows.count returns 1, even when there are no rows with data.

Hi @meaganlord ,
Later i will send sample xaml file for your issue

Happy learning

Have you used the build data table activity to initialise the table? If so, that comes with one default row as standard. Go to the activity click on datatable and delete all rows under the headers using the cross on the left hand side.

I just tried this with mine and it returned a DT size of 1 first, then a size of 0 when I deleted the default row in the activity

@pattyricarte thanks! I would be interested to see your solution. No rush, as the Filter Data Table activity is working just fine for me.

@AndyD I am using the Build Data Table activity, but it does not have the default row. Good idea, as I hadn’t thought of checking that.

Hi @meaganlord

can i see your xaml file for further investgate… Thanks

cheers :smiley:

Happy learning :smiley:

Here is an detailed article on it :slight_smile:
# How To Count Rows In DataTable – In UiPath


1 Like