如何将值添加到从其他表计算的表中 [英] how to add values into a table which is calculated from other table
本文介绍了如何将值添加到从其他表计算的表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何通过计算其他表中的条目来向表中添加条目。我在table1的每个学科都有成绩。我有另一个表table2,我想在其中计算学生的平均分数并查看它。我怎么能这样做,我在视觉工作室使用c#进行编码。
添加评论表
表1
How to add entries to a table by calculating the entries in other table. I have marks got in every subject for a student in table1 respectiveley. I have another table table2 in which i want to calculate the average marks of the student and view it. How can I do it, I am coding using c# in visual studio.
Added table from comment
Table 1
studentnumber subjectcode marks
1 2 75
1 3 65
2 2 50
2 3 75
1 1 65
推荐答案
认为你已经有了表(DataGridview)学生标记。您需要读取这些标记并计算平均标记。然后在不同的表格中显示平均值。
从表格中读取学生标记(Datagridview):
Consider you already have table(DataGridview) with students marks. You need read those marks and calculate average marks. Then display the average in different table.
Reading students marks from table(Datagridview):
string studentName = "";
int english, maths, science, average;
BindingSource bs = (BindingSource) dataGridView1.DataSource;
// Converting datagridview datasource to data table
DataTable dataTable = (DataTable) bs.DataSource;
//Creating datatable for 2nd table
DataTable dt = new DataTable();
dt.Columns.Add("Student_Name");
dt.Columns.Add("Average");
foreach (DataRow dr in dataTable.Rows) // reading each row from datatable
{
studentName = dr["studentName"].ToString();
english = Convert.ToInt32(dr["English"]);
maths = Convert.ToInt32(dr["Maths"]);
science = Convert.ToInt32(dr["Science"]);
//Calculating AVERAGE
average = (english + maths + science)/3;
//Adding students average in datatable
DataRow row = dt.NewRow();
row["Student_Name"] = studentName;
row["average"] = average;
dt.Rows.Add(row);
}
//bind datatable with the 2nd table(datagridview)
dataGridView2.DataSource = dt;
这篇关于如何将值添加到从其他表计算的表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文