I Use the following code for load the sql table values into the datatable
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("Connection String Here");
con.Open();
SqlCommand cmd = new SqlCommand("select * from ExportExcel", con);
dt.Load(cmd.ExecuteReader());
int total = 0;
foreach (DataRow row in dt.Rows)
{
int salaryvalue = Convert.ToInt32(row["Salary"]);
total = salaryvalue + total;
}
dt.Rows.Add("Salary");
dt.Rows.Add(total);
And I add Salary,total dynamically.But It Shows in first column one by one.
But I need salary in Department column,and total in Salary Column.How to do this?
you can use
dt.rows.add(new object[]{"","Salary",total });
Instead of calculating Total you can use Datatable.Compute Method
object total;
total= dt.Compute("Sum(Salary)", "");
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments