I want to get the sum of only two columns for each data row in datable and display it in datagridView. I tried to use link query:
SqlCommand cmd = new SqlCommand("SELECT * FROM Student", con);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
dataGridView1.Rows.Add(dt.Rows.Count);
foreach(DataRow row in dt.Rows)
{
dataGridView1.CurrentRow.Cells["Column1"].Value = row["FullName"].ToString();
dataGridView1.CurrentRow.Cells["Column2"].Value = row["Maths"].ToString();
dataGridView1.CurrentRow.Cells["Column3"].Value = row["Physics"].ToString();
dataGridView1.CurrentRow.Cells["Column4"].Value = dt.AsEnumerable().Sum(r => double.Parse(r[1].ToString()) + double.Parse(r[2].ToString()));
}
This code throws this exception:
The format of the input string is incorrect.
Any suggestions?
Immediate Window. Type in?r[1].ToString(). What is displayed? Type in?r[2].ToString(). What is displayed?ris coming from? What if you changer[1]torow["Maths"]andr[2]torow["Physics"]?