Find last row by refer column name

Hello,

I have datatable.
I want find last row column ID and column Name
by not user loop

Please guide me for solve it.

Thank you

@Stef_99,

Can you elaborate this more?

Hi @Stef_99

I am a bit confused, you want the last row based on Column Name??

@Stef_99 you mean you wants to get the cell value of last row by referring the column name? if so you can use the below linq

dt.AsEnumerable().Last()("ColumnName").ToString

Hi @Stef_99

You can get the last column values this way

columnId = dt_input.Rows(dt_input.RowCount-1)(“ColumnId”)

columnName = dt_input.Rows(dt_input.RowCount-1)(“ColumnName”)

Hope this helps!

@Stef_99

if you need last column name then try this

dt.Columns(dt.ColumnCount-1).ColumnName will give the last column name

if you mean the index when you say ID then dt.ColumnCount-1 will give it

cheers

Example data

I want last number row column ID =5
last number row column Name = 3

Thank you

you can use this linq
dt.AsEnumerable().Last()(dt.ColumnCount-1).ToString

@sppal.c I don’t want value in last row.

Expect data

last number row column ID =5
last number row column Name = 3

@Stef_99

Can you check this

lastColumnIdrownumber = dt_input.AsEnumerable().ToList().FindIndex(Function(x) String.IsNullOrWhiteSpace(x("ID").ToString())).ToString

lastrowNamenumber = dt_input.AsEnumerable().ToList().FindIndex(Function(x) String.IsNullOrWhiteSpace(x("Name").ToString())).ToString

Hope this helps!

1 Like

@Stef_99,

This should give you Last Non-Empty Row Number for Column “ID”

dt.AsEnumerable().Select(Function(row, index) New With {Key .Index = index, Key .Row = row}).Reverse().First(Function(x) Not String.IsNullOrWhiteSpace(x.Row("ID").ToString)).Index + 2

This will give you Last Non-Empty Row Number for Column “Name”

dt.AsEnumerable().Select(Function(row, index) New With {index, row}).Reverse().FirstOrDefault(Function(x) Not String.IsNullOrWhiteSpace(x.row("Name").ToString)).index + 2

Input:

Output:

1 Like

output now show -1

Not true

@Stef_99 you can directly use the count method.

dt.RowCount-1 will be equals to the ID of last row

dt.ColumnCount-1 will the the ID of last column in your datatable

1 Like

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