Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.

[EDIT]Added table from comment

Table 1
studentnumber subjectcode marks
1             2           75
1             3           65
2             2           50
2             3           75
1             1           65
Posted
Updated 3-Sep-15 5:14am
v2
Comments
Sarath kumar.N 3-Sep-15 2:27am    
Once you got the values from one table try create data table. After creating data table you can use use it anywhere.
Member 11949886 3-Sep-15 2:29am    
Thank u sarath bhai.. but can u please tell what is the command I can write?? I am stuck in this from 2 days
Sarath kumar.N 3-Sep-15 3:05am    
without knowing the structure how can I write the code for you? You have to mention structure of 1st table. Then what do you want display in a second table. that will be easier to help you even others.
Member 11949886 3-Sep-15 7:56am    
Sarath sir, I have given structure of the table... Please have a look at it
Sarath kumar.N 3-Sep-15 8:56am    
Give 10 min. I will be with solution.Thanks!

1 solution

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):

C#
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;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900