集合列表和gridview

时间:2015-10-03 08:07:11

标签: c#

我想添加Mark1,Mark2,Mark3,Mark4,Mark5并将其分配给Sum以显示在gridview中。

我想通过任何函数添加这些标记并将其分配给Sum属性。怎么做?感谢

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void procees()
    {            
        List<student> ml= new List<student>();

        student s1 = new student() {
            Name = "ram", 
            Id = "gn01", 
            Mark1 = 90, 
            Mark2 = 89,
            Mark3 = 75,
            Mark4 = 45,
            Mark5 = 65,
            Sum = "" 
        };
        ml.Add(s1);

        dataGridView1.DataSource = ml; 
    }


    private void button1_Click(object sender, EventArgs e)
    {
        procees();
    }
 }

 public class student
 {
     private string name;

     public string Name
     {
         get { return name; }
         set { name = value; }
     }

     private string id;

     public string Id
     {
         get { return id; }
         set { id = value; }
     }

     private int mark1;

     public int Mark1
     {
         get { return mark1; }
         set { mark1 = value; }
     }
     private int mark2;

     public int Mark2
     {
         get { return mark2; }
         set { mark2 = value; }
     }
     private int mark3;

     public int Mark3
     {
         get { return mark3; }
         set { mark3 = value; }
     }
     private int mark4;

     public int Mark4
     {
         get { return mark4; }
         set { mark4 = value; }
     }
     private int mark5;

     public int Mark5
     {
         get { return mark5; }
         set { mark5 = value; }
     }
     private int sum;

     public int Sum
     {
         get { return sum; }
         set { sum = value; }
     }
 }

1 个答案:

答案 0 :(得分:1)

删除私人字段&#39;总结&#39;:

 private int sum;

让公共财产&#39; Sum&#39;计算总和:

 public int Sum
 {
     get { return Mark1 + Mark2 + Mark3 + Mark4 + Mark5; }
 }