集合列表和网格视图

本文关键字:视图 网格 列表 集合 | 更新日期: 2023-09-27 18:15:44

我想添加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; }
     }
 }

集合列表和网格视图

删除私有字段'sum':

 private int sum;

让公共属性'Sum'计算总和:

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