Hi All ,
How to find the Last row of the Table row , to validate something on that last row .
can any one suggest with sample code .
for (int i = 0; i < DT.Rows.Count; i++)
{
DataRow dr = DT.Rows[i];
RBranchid = dr["Branchid"].ToString();
}
Thanks in advance
Karthik.K


Eliana BlakeRank #1,595 · 993 reputationPosted Apr 24, 2025, 11:32 PM EDT
Hello Karthik,
To find the last row of a DataTable within a for loop, you can simply adjust your loop to iterate until the second-to-last row and then handle the last row separately. Here's how you can modify your existing code snippet to achieve this:
for (int i = 0; i < DT.Rows.Count - 1; i++) { DataRow dr = DT.Rows[i]; RBranchid = dr["Branchid"].ToString(); } // Handling the last row separately DataRow lastRow = DT.Rows[DT.Rows.Count - 1]; // Perform your validation or any required actions on the last row here RBranchid = lastRow["Branchid"].ToString();In this updated code, the for loop iterates until the second-to-last row of the DataTable (DT.Rows.Count - 1) to process all rows except the last one. Then, outside the loop, you can access and work with the last row for validation or any specific requirements.
This approach ensures that your for loop processes all rows except the last one and then allows you to handle the last row differently. It helps in isolating the last row for specific operations without affecting the main loop logic.
I hope this explanation and code snippet provide a clear guide on how to find and handle the last row of a DataTable in a loop efficiently. If you have any further questions or need additional assistance, feel free to ask!
Best regards,