jithesh
jithesh

Reputation: 253

how to get a last column value in a Templorary datatable (.net)

I am storing some values in a temporary datatable.Sample values as follows

ID FILENAME         PATH
--------------------------
1   txt1            C:\NewFolder
2   txt2            C:\NewFolder
3   txt3            C:\NewFolder

I want to get the last value of ID column.

Upvotes: 0

Views: 1999

Answers (2)

Jose Tuttu
Jose Tuttu

Reputation: 428

      string expression = "1=1"

      // Sort descending by column named CompanyName. 
      string sortOrder = "ID DESC";
      DataRow[] foundRows;

      // Use the Select method to find all rows matching the filter.
      foundRows = table.Select(expression, sortOrder);
      var row = foundRows[0];

Ref:https://msdn.microsoft.com/en-us/library/way3dy9w(v=vs.110).aspx

Upvotes: 1

dotnetom
dotnetom

Reputation: 24901

You can use such code to get the last row and then the value from column ID:

object lastId = table.Rows[table.Rows.Count-1]["ID"];

If by last you meant that you need the maximum value from the table you can use the following LINQ query to get the result:

int maxValue= table.AsEnumerable().Select(row => Convert.ToInt32(row["ID"])).Max();

You need to have the following using in order for it to work:

using System.Data.DataSetExtensions;

Upvotes: 3

Related Questions