Dt.select using column index instead of column name

Hi,

I am aware of the following method:

dt.Select("[Column Name] ="+"’"+blah+"’" ).CopyToDataTable

but I have a dynamic column name so using column index will be a better approach.
I got the column name using:

cName = dt.Columns(10).ColumnName

can someone direct me who to put this variable in dt.select?

I tried

dt1.Select(credit “> 0”).CopyToDataTable

dt1.Select(“credit > 0”).CopyToDataTable

They don’t work…

Thanks!
Lavina

Edit: I actually got it! ha - for those who have the same problem:

dt1.Select("[" + cName + “] > 0”).CopyToDataTable

@lavint

dt.Select("[Column Name] =’"+dt.Columns(Column_Index).ColumnName.ToString+"’" ).CopyToDataTable

Regards,
Mahesh

Hello Mahesh ,

If the excel file does not have headers and I want to filter data on the basis of column index , then could you please tell me how to do it?

@AkankshaP

First Read the Excel sheet and store it in a DataTable. Let us take as dta.

Now Filter the DataTable by using below Query.

dtb= (From p In dta.Select
        Where p(Column_Index).ToString.Equals("StringValue")
        Select p).ToArray.CopyToDataTable

dtb is the Filtered DataTable.

Column_Index-> integer

Regards,
Mahesh

3 Likes

Hello Mahesh ,

Thanks. I tried this solution , and its working fine.
Thanks for your instant support.

Regards,
Akanksha

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