如何根据第一栏中的选择来计算CSV中的项目?

如何解决如何根据第一栏中的选择来计算CSV中的项目?

您好,我的程序是邮资计算器,我在第一列中计算了包裹重量,在第二列中计算了成本。用户从一个下拉列表中选择他们的体重,然后我想从第1列*第2列等效值中计算出weight.selected索引。在第三列做同样的事情

csv是

20公斤£16.50£23.80

35公斤£29.40£36.20

60公斤£41.60£45.10

85公斤£52.40£61.20

100公斤£66.20£74.80

100kg + 0.65 0.8

例如用户选择20kg作为包裹重量,我想将£16.50设置为总费用

输入表单代码

Repository repo = new FileRepositoryBuilder() 
   .findGitDir(new File("C:\\work"))
   .setWorkTree(new File("C:\\work"))
   .build(); 

Git git  = new Git(repo); 
git
   .reset()
   .setMode(ResetType.HARD)
   .call(); 
git.close(); 

}

用于输出结果的表格2

namespace ParcelDelivery
{
    public partial class Postage : Form
    {
    List<string> weight = new List<string>();
    List<string> StandardUK = new List<string>();
    List<string> SurfaceEU = new List<string>();
    public Postage()
    {
        InitializeComponent();

        StreamReader reader = File.OpenText("PostageCost.csv");
        while (!reader.EndOfStream)
        {
            String line = reader.ReadLine();
            String[] values = line.Split(',');

            cBxWeightUk.Items.Add(values[0]);
            cBxWeightEU.Items.Add(values[0]);
            StandardUK.Add(values[1]);
            SurfaceEU.Add(values[2]);

        }
        reader.Close(); //close the csv file

        gBxDeliveryEU.Visible = false;
        gBxPostageDetsEU.Visible = false;
        gBxDeliveryUK.Visible = false;
        gBxPostDetsUK.Visible = false;
        gBxInsurance.Visible = false;
    }

    private void rBtnUK_CheckedChanged(object sender,EventArgs e)
    {
        gBxDeliveryEU.Visible = false;
        gBxPostageDetsEU.Visible = false;
        gBxDeliveryUK.Visible = true;
        gBxPostDetsUK.Visible = true;
        gBxInsurance.Visible = true;
    }

    private void rBtnEU_CheckedChanged(object sender,EventArgs e)
    {
        gBxDeliveryUK.Visible = false;
        gBxPostDetsUK.Visible = false;
        gBxDeliveryEU.Visible = true;
        gBxPostageDetsEU.Visible = true;
        gBxInsurance.Visible = true;
    }

    private void btnExit_Click(object sender,EventArgs e)
    {
        //when exit button is clicked user will get a message box asking if they want to exit,if yes program closes
        DialogResult answer = MessageBox.Show("Would you like to exit?","Exit",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
        if (answer == DialogResult.Yes)
        {
            //close program
            Application.Exit();
        }
    }

    private void btnClear_Click(object sender,if yes program closes
        DialogResult answer = MessageBox.Show("Would you like to clear all infortmation entered?",MessageBoxIcon.Warning);
        if (answer == DialogResult.Yes)
        {
            cBxTitleEu.SelectedIndex = -1;
            txtFNameEu.Text = "";
            txtLastnameEu.Text = "";
            txtHouseNoEu.Text = "";
            txtPostcodeEu.Text = "";
            txtStreetEu.Text = "";
            cBxCountriesEu.SelectedIndex = -1;
            cBxWeightEU.SelectedIndex = -1;
            txtKilosEu.Text = "";

            cBxTitleEu.Focus();

            cBxTitlesUk.SelectedIndex = -1;
            txtFNameUk.Text = "";
            txtLNameUk.Text = "";
            txtHouseNo.Text = "";
            txtPostcode.Text = "";
            txtStreet.Text = "";
            cBxCountriesUk.SelectedIndex = -1;
            cBxWeightUk.SelectedIndex = -1;
            txtKilosUk.Text = "";

            cBxTitlesUk.Focus();

        }
    }

    private void cBxWeightUk_SelectedIndexChanged(object sender,EventArgs e)
    {
        if (cBxWeightUk.SelectedIndex == 5)
        {
            lblKilosUk.Visible = true;
            txtKilosUk.Visible = true;
        }
        //double weightUK = Convert.ToDouble(txtKilosUk.Text);
        //txtKilosUk.Text = Convert.ToDouble(weightUK);

        

    }

    private void cBxWeightEU_SelectedIndexChanged(object sender,EventArgs e)
    {

        if (cBxWeightEU.SelectedIndex == 5)
        {
            lblKilosEu.Visible = true;
            txtKilosEu.Visible = true;
        }

        //double weightEU = Convert.ToDouble(txtKilosEu.Text);

    }

    private void btnCalc_Click(object sender,EventArgs e)
    {
        int position = postDets.weight;
        details();
        calculateTotal(position);
        Form frmDelivery = new DeliveryCost();
        frmDelivery.Show();
        this.Hide();
    }

    private void details()
    {
        if (rBtnUK.Checked == true)
        {
            postDets.UK = true;
            postDets.title = cBxTitlesUk.Text;
            postDets.fName = txtFNameUk.Text;
            postDets.sName = txtLNameUk.Text;
            postDets.houseNo = txtHouseNo.Text;
            postDets.street = txtStreet.Text;
            postDets.country = cBxCountriesUk.Text;
            postDets.postcode = txtPostcode.Text;
            postDets.weight = cBxWeightUk.SelectedIndex;
            postDets.kilos = txtKilosUk.Text;

            if (rBtnNextDay.Checked == true)
            {
                postDets.extra = 3.50;
                postDets.delSelection = "Next day selected";
            }
            else
            {
                postDets.extra = 0;
                postDets.delSelection = "Standard selected";
            }

            if (rBtnYinsurance.Checked == true)
            {
                postDets.insurance = 0.10;
                postDets.inSelection = "Insurance selected";
            }
            else
            {
                postDets.insurance = 0;
                postDets.inSelection = "Insurance not selected";
            }

        }
        else if (rBtnEU.Checked == true)
        {
            postDets.UK = false;
            postDets.title = cBxTitleEu.Text;
            postDets.fName = txtFNameEu.Text;
            postDets.sName = txtLastnameEu.Text;
            postDets.houseNo = txtHouseNoEu.Text;
            postDets.street = txtStreetEu.Text;
            postDets.country = cBxCountriesEu.Text;
            postDets.postcode = "N/A";
            postDets.weight = cBxWeightEU.SelectedIndex;
            postDets.kilos = txtKilosEu.Text;

            if (rBtnAirMail.Checked == true)
            {
                postDets.extra = 8.50;
                postDets.delSelection = "Air mail selected";
            }
            else
            {
                postDets.extra = 0;
                postDets.delSelection = "Surface mail selected";
            }

            if (rBtnYinsurance.Checked == true)
            {
                postDets.insurance = 0.10;
                postDets.inSelection = "Insurance selected";
            }
            else
            {
                postDets.insurance = 0;
                postDets.inSelection = "Insurance not selected";
            }
        }
    }

   private double calculateTotal(int position)
    {
        double price,cost;
        

        if (postDets.UK == true)
        {
            price = double.Parse(StandardUK[position]);
        }
        else
        {
            price = double.Parse(SurfaceEU[position]);
        }

       
        cost = price * postDets.insurance + postDets.extra;
        return cost;
    }
}

namespace ParcelDelivery
{
    public partial class DeliveryCost : Form
    {
    List<string> Weight = new List<string>();
    List<string> StandardUK = new List<string>();
    List<string> SurfaceEU = new List<string>();
    public DeliveryCost()
    {
        InitializeComponent();

        StreamReader reader = File.OpenText("PostageCost.csv");
        while (!reader.EndOfStream)
        {
            String line = reader.ReadLine();
            String[] values = line.Split(',');
            Weight.Add(values[0]);
            StandardUK.Add(values[1]);
            SurfaceEU.Add(values[2]);

        }
    }
    private void btnVeiw_Click(object sender,EventArgs e)
    {
        lblNameAddDets.Text = postDets.title + " " + postDets.fName + " " + postDets.sName + "\r" + postDets.houseNo + " " + postDets.street + "\r" + postDets.country + "\r" + postDets.postcode + "\r" + postDets.weight + " Kg " + postDets.kilos + " Kilos";
        lblPostDets.Text = postDets.inSelection + "\r" + postDets.delSelection;
        lblCostResult.Text = calculateTotal();
    }

    private void btnNew_Click(object sender,EventArgs e)
    {
        Form frmSplash = new frmPDsplash();
        frmSplash.Show();
        this.Hide();
    }

    private void btnExit_Click(object sender,MessageBoxIcon.Warning);
        if (answer == DialogResult.Yes)
        {
            //close program
            Application.Exit();
        }
    }

解决方法

根据您的描述,您想要使用csv文件作为combobox的数据源。 您可以尝试以下代码:

using LumenWorks.Framework.IO.Csv;
using System;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace ComboCsv
{
public partial class Form1 : Form
{
    DataTable dta;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender,EventArgs e)
    {

        Regex re = new Regex(@"[^\d.]"); 
        double strweight = Convert.ToDouble(re.Replace(comboBox1.Text,""));
        double strprice1 = Convert.ToDouble(re.Replace(txtprice1.Text,""));
        double strprice2 = Convert.ToDouble(re.Replace(txtprice2.Text,""));          
        txtcalcu1.Text = (strweight*strprice1).ToString();
        txtcalcu2.Text = (strweight*strprice2).ToString();
    }

    private void Form1_Load(object sender,EventArgs e)
    {
        dta= GetStream(@"....\xx.csv");
        comboBox1.DataSource = dta;
        comboBox1.DisplayMember = "weight";
    }
    public static DataTable GetStream(string FilePath)
    {
        FileStream fileStream = new FileStream(FilePath,FileMode.Open,FileAccess.Read,FileShare.Read);
        byte[] bytes = new byte[fileStream.Length];
        fileStream.Read(bytes,bytes.Length);
        fileStream.Close();
        Stream stream = new MemoryStream(bytes);
        return GetData(stream);
    }
    private static DataTable GetData(Stream stream)
    {
        using (stream)
        {
            using (StreamReader input = new StreamReader(stream,Encoding.GetEncoding("GBK")))
            {
                using (CsvReader csv = new CsvReader(input,true))
                {
                    DataTable dt = new DataTable();
                    //Number of fields in the first row
                    int columnCount = csv.FieldCount;
                    //Title array
                    string[] headers = csv.GetFieldHeaders();
                    //Add header row in loop
                    for (int i = 0; i < columnCount; i++)
                    {
                        dt.Columns.Add(headers[i]);
                    }
                    //Add column data in a loop
                    while (csv.ReadNextRecord())
                    {
                        DataRow dr = dt.NewRow();
                        for (int i = 0; i < columnCount; i++)
                        {
                            if (!string.IsNullOrWhiteSpace(csv[i]))
                            {
                                dr[i] = csv[i];
                            }
                        }
                        dt.Rows.Add(dr);
                    }
                    return dt;
                }
            }
        }
    }

    private void comboBox1_SelectedIndexChanged(object sender,EventArgs e)
    {
        string str = comboBox1.Text;
        DataRow[] rows = dta.Select("weight='" + str + "'");
        foreach (var item in rows)
        {
            txtprice1.Text = item["price1"].ToString();
            txtprice2.Text = item["price2"].ToString();
        }
    }
}
}

我的csv文件:

here

运行结果:

here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-