C# DataGridView&内嵌窗口&XML文件解析综合运用

C# DataGridView&内嵌窗口&XML文件解析综合运用

PS: 也是从网上各个帖子中学习的C#,因此代码的格式以及内容有粘贴网上其他大神的代码,如有侵权请告知删除

程序界面:
在这里插入图片描述
程序功能:
有两个xml文件,分别记录学生的分数和爱好,程序的作用是读取两个文件,并在界面上显示出来,别允许用户在界面上对内容进行修改,点击保存后生效在对应文件上。

程序涉及控件:
ListView;DataGridView;Button;Panel

程序目的:
了解以上控件的实际应用

XML文件格式:
在这里插入图片描述

程序结构:
在这里插入图片描述
程序代码:
Hobby.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormTest
{
   
    public partial class Hobby : Form
    {
   
        public Hobby()
        {
   
            InitializeComponent();
        }

        private void Hobby_Load(object sender, EventArgs e)
        {
   
            //读取对应的XML文件,对对应的数组进行赋值,然后在界面显示
            File.ReadHobbyFile();
            for (int i = 0; i < File.HobbyName.Length; ++i)
            {
   
                if (File.HobbyName[i] != null)
                {
   
                    dataGridView1.Rows.Add();
                    dataGridView1.Rows[i].Cells[0].Value = File.HobbyName[i];
                    dataGridView1.Rows[i].Cells[1].Value = File.Hobby[i];
                }
            }
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
   
            //界面上修改了什么内容,就对对应的数组进行更改,方便保存功能的实现
            switch (e.ColumnIndex)
            {
   
                case 0:
                    if (e.ColumnIndex > -1 && e.RowIndex > -1)
                    {
   
                        File.HobbyName[e.RowIndex] = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
                    }
                    break;
                case 1:
                    if (e.ColumnIndex > -1 && e.RowIndex > -1)
                    {
   
                        File.Hobby[e.RowIndex] = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
                    }
                    break;
            }
        }
    }
}

Score.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormTest
{
   
    public partial class Score : Form
    {
   
        public Score()
        {
   
            InitializeComponent();
        }

        private void Score_Load(object sender, EventArgs e)
        {
   
            //读取对应的XML文件,对对应的数组进行赋值,然后在界面显示
            File.ReadScoreFile();
            for(int i = 0;i<File.ScoreName.Length;++i)
            {
   
                if (File
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值