How to check if a particular column is empty in DataTable (Without using For Each Row)?

datatable
excel
studio

#1

Without using For Each Row loop, How can we check if a column is empty or not in DataTable ??
I want to delete a column if it is empty and I acheived it with the help of For Each loop. Can anyone help me in solving this without loop?


#2

In If Condition Check
DataTable.Rows(0)(“columnname”) is DBNull.Value then Delete a column


#3

for each column in dt
if dt.Select(dt.Columns(index or columnname)=dbnull).Length=dt.Rows.Count
then msgbox “column is empty”
end if
next


#4

@KinjalRD @Atrimal Thank you! I already tried this way but not worked.

error : DBNull is a type and cannot be used as an expression.

Can you please check again?

Thanks!


#5

Hi Santhosh,

You can try this out.


#6

Check if,

DataTable.Rows(0)(“columnname”).ToString = “”


#7

dt.Select(dt.Columns(index or columnname).tostring="").Length=dt.Rows.Count


#8

@RupeshGonte Thank you!

DataTable.Rows(0)(“columnname”).ToString = ""
this will check only for the first row , I need it for whole data table without using any loop. can you try ?


#9

You can try using below linq query
(From row In dt where row(“id”) isnot DBNull.Value).CopyToDatatable

Or You can assign it to array of DataRow

datarow[] arr = (From row In dt where row(“id”) isnot DBNull.Value).toArray
and check
if( arr.count=0)


#10

@SantoshPothina PFB