基本信息
源码名称:C#编写的油耗统计
源码大小:0.17M
文件格式:.zip
开发语言:C#
更新时间:2025-10-10
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 1 元 
   源码介绍

取txt文件,到表格,表格在计算得到写入表格,表格内容写入图标


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.IO;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string filePath = openFileDialog.FileName;
                    try
                    {
                        // Read the file content into a string array.
                        string[] lines = File.ReadAllLines(filePath);

                        // Clear existing data in DataGridView.
                        dataGridView1.Rows.Clear();

                        foreach (string line in lines)
                        {
                            // Split the line by commas and add it as a row to the DataGridView.
                            string[] fields = line.Split(',');
                            dataGridView1.Rows.Add(fields);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error reading file: "   ex.Message);
                    }
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // Initialize columns for the DataGridView based on expected number of comma-separated values.
            dataGridView1.ColumnCount = 3; // Assuming there are three fields per line.
            dataGridView1.Columns[0].HeaderText = "公里";
            dataGridView1.Columns[1].HeaderText = "金额";
            dataGridView1.Columns[2].HeaderText = "加油量";

            // Set properties for better user experience.
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }

        private void Form1_Load_1(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            dataGridView2.Rows.Clear();
            dataGridView2.Columns[3].HeaderText = "百公里油耗";
            int count = 0;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (dataGridView1.Rows[count   1].Cells[0].Value != null && int.TryParse(row.Cells[0].Value.ToString(), out _))
                {
                    double a = Convert.ToDouble(dataGridView1.Rows[count   1].Cells[0].Value);
                    double b = Convert.ToDouble(dataGridView1.Rows[count].Cells[0].Value);
                    double d = Convert.ToDouble(dataGridView1.Rows[count].Cells[1].Value);
                    double e1 = Convert.ToDouble(dataGridView1.Rows[count].Cells[2].Value);
                    string[] abc = { String.Format("{0}", a - b), String.Format("{0:F3}", d / (a - b)), String.Format("{0:F3}", e1 / (a - b)), String.Format("{0:F2}", (e1 / (a - b))* 100 ) };
                    dataGridView2.Rows.Add(abc);
                    count  ;
                }
            }
        }
    }
}