Hi rani,
Check this example. Now please take its reference and correct your code.
Controller
public class HomeController : Controller
{
public IActionResult Index()
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[] {
new System.Data.DataColumn("Year", typeof(string)),
new System.Data.DataColumn("ToBill",typeof(decimal)),
new System.Data.DataColumn("Billed",typeof(decimal))});
dt.Rows.Add(2011, 1000, 500);
dt.Rows.Add(2012, 2500, 100);
dt.Rows.Add(2013, 100, 50);
dt.Rows.Add(2014, 1000, 500);
dt.Rows.Add(2015, 400, 200);
dt.Rows.Add(2016, 3000, 2000);
dt.Rows.Add(2017, 2500, 100);
dt.Rows.Add(2018, 100, 50);
dt.Rows.Add(2019, 2500, 100);
dt.Rows.Add(2020, 100, 50);
dt.Columns.Add("Percentage");
for (int i = 0; i < dt.Rows.Count; i++)
{
System.Data.DataRow dr = dt.Rows[i];
dr["Percentage"] = Math.Round((Convert.ToDecimal(dr["Billed"]) / Convert.ToDecimal(dr["ToBill"])) * 100);
}
ViewBag.Data = dt;
return View();
}
}
View
@using System.Data;
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table>
<tr>
@foreach (DataColumn column in ViewBag.Data.Columns)
{
<th>@column.ColumnName</th>
}
</tr>
@foreach (DataRow row in ViewBag.Data.Rows)
{
<tr>
@foreach (DataColumn column in ViewBag.Data.Columns)
{
<td>@row[column]</td>
}
</tr>
}
</table>
</body>
</html>
Screenshot