Reputation: 253
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
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
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