In below Foreach loop
foreach (DataRow row in dt.Rows)
{
   foreach (DataColumn col in dt.Columns)
   {
       var val=row[col].ToString()
       var index=??
   }
}
How to check if current column is last column ?
In below Foreach loop
foreach (DataRow row in dt.Rows)
{
   foreach (DataColumn col in dt.Columns)
   {
       var val=row[col].ToString()
       var index=??
   }
}
How to check if current column is last column ?
 
    
     
    
    If i Understand you correctly , the thing you looking for is Ordinal
    foreach (DataRow row in dt.Rows)
     {
       foreach (DataColumn col in dt.Columns)
         {
           var index = col.Ordinal+1; // Current column index
           if(index == dt.Columns.Count) // if column is last column in current row
              {
                your logice goes here
              }
          }
     }
It gives you current position of column item
check this MSDN Link
 
    
    You can follow this as well:
foreach (DataRow row in dt.Rows)
{         
     for(int i=0; i< dt.Columns.Count; i++)
     {
        var val=row[i].ToString()
        // here i will be the index
        // dt.Columns[i] will be the column
     }
}
If your requirement is to do something with last column[as mentioned in the question] means need not to go ahead with iteration, you can make use of the column index like this: row[dt.Columns.Count-1]
 
    
    public static void DataD() {
        DataTable table = new DataTable();
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Patient", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));
        // Here we add five DataRows.
        table.Rows.Add(25, "Indocin", "David", DateTime.Now);
        table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
        table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
        table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
        table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
        int count = table.Columns.Count;
        var lastColumn = table.Columns[count - 1];
        string columnname = lastColumn.ColumnName;
        foreach (DataRow row in table.Rows)
        {
            foreach (DataColumn col in table.Columns)
            {
                var val = row[col].ToString();
                var index = table.Columns.IndexOf(col);
                if (columnname == col.ColumnName) {
                    // this is my last colums
                }
            }
        }
    }
 
    
    This should help you
var index,val;
foreach (DataRow row in dt.Rows)
{
 foreach (DataColumn col in dt.Columns)
   {
      index=dt.Columns.IndexOf(col);
      val=row[index].ToString();
    if (index == dt.Columns.Count-1)
        //your code here...
   }
}
 
    
    Here try this.
foreach (DataRow row in dt.Rows)
  {
   var index =0;
   foreach (DataColumn col in dt.Columns)
       {
        var val=row[col].ToString()
          index++;
          if(index == dt.Columns.Count)
            //true
       }
  }
 
    
    Use the following peace of code
int total=dt.Columns.Count;
int i=0;
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn col in dt.Columns)
   {
if(i==total-1)
{
}
i++;
    var val=row[col].ToString()
    var index=??
   }
}
