Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从csv文件读取数据以存储在int list c#windows窗体中_C#_Winforms_List_Csv - Fatal编程技术网

从csv文件读取数据以存储在int list c#windows窗体中

从csv文件读取数据以存储在int list c#windows窗体中,c#,winforms,list,csv,C#,Winforms,List,Csv,我现在正在尝试为学院创建一个windows窗体应用程序,以计算和显示从csv文件读取的邮资成本 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.Win

我现在正在尝试为学院创建一个windows窗体应用程序,以计算和显示从csv文件读取的邮资成本

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

namespace ParcelEXDelivery
{
    public partial class frmParcelCalculator : Form
    {

        int price, insurance;
        string file;
        List<int> UK = new List<int>();
        List<int> RestOfEurope = new List<int>();
        List<int> Worldwide = new List<int>();


        public frmParcelCalculator()
        {
            InitializeComponent();


        }

        private void destination()
        {

            if (rbUK.Checked)
            {

                file = "..\\Debug\\PostageCosts.csv";
                var reader = new StreamReader(file);
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    var values = line.Split(',');

                    UK.Add(int.Parse(values[0]));

                }
                reader.Close();
                display();
            }
        }

        public void display()
        {
            label2.Text = UK[price].ToString();

        }
我能做些什么来解决这个问题?还是我把这一切都搞错了?我试图在标签中显示,只是为了看看是否输出了正确的邮资成本

16.5    27.3    41.6    52.4    66.2    0.6 per kg  UK
21.5    31.3    45.1    58.5    71.5    0.7 per kg  Rest of Europe
27.4    37.2    50.3    62.4    76.3    0.75 per kg Worldwide
这是csv文件

private void destination()
        {

            if (rbUK.Checked)
            {
                var file = "..\\Debug\\Questions.csv";
                if (File.Exists(file))
                {
                    var f = File.OpenRead(file);
                    var reader = new StreamReader(f);

                    while (!reader.EndOfStream)
                    {
                        var row = reader.ReadLine();
                        if (row != null)
                        {
                            var values = row.Split(',');

                            foreach (var item in values)
                            {

                                if (decimal.TryParse(values[0], out Value))
                                {
                                    UK.Add(Value);
                                }
                                else
                                {

                                }
                            }
                        }
                    }
                }
            }
        }
这是上面和下面的编辑方法,是我尝试将值输出到标签时的代码,它表示无法从十进制转换为int

        public void display()
        {
            label2.Text = UK[Value].ToString();

        }

是的,你设置得非常错误。主要问题是你的价值观。您需要解析的是十进制数,而不是整数。因此,您的列表应该反映这一基本情况。创建
列表
列表

编辑

您读取文件的方式也有一些问题需要解决。
似乎有三行,第一行中的值应该放在第一个列表(英国)中,第二行中的值应该放在第二个列表(RestOfEurope)中,第三行放在全球列表中。我建议一起阅读所有行,然后在适当的列表中指定值

private void destination()
{

    if (rbUK.Checked)
    {
        // Read all the lines in a single call, 
        string[] lines = File.ReadAllLines(@".\Debug\PostageCosts.csv");

        // I expect to have three lines but better to check before acting on the array

        if(lines.Length > 0)
        {
            // Split the line for UK and use only the first 6 values
            // for the list (the last strings 'per kg  UK' should not be added)
            // Again, at least 6 decimal values are expected not less...
            var values = lines[0].Split(',');
            if(values.Length >= 6)
                for(int x = 0; x < 6; x++)
                    UK.Add(decimal.Parse(values[x]));
        }
        if(lines.Length > 1)
        {
            var values = lines[1].Split(',');
            if(values.Length >= 6)
                for(int x = 0; x < 6; x++)
                    RestOfEurop.Add(decimal.Parse(values[x]));
        }
        if(lines.Length > 2)
        {
            var values = lines[2].Split(',');
            if(values.Length >= 6)
                for(int x = 0; x < 6; x++)
                    WorlWide.Add(decimal.Parse(values[x]));
        }
        // Test what list to display passing the index 0,1,2
        int listToShow = 0;
        display(listToShow);
    }
}
public void display(int listToShow)
{
    List<decimal> showList = (listToShow == 0 ? UK : 
                              listToShow == 1 ? RestOfEurope : 
                                                WorlWide);
    label2.Text = string.Join("-",  showList.ToArray());
}
private void destination()
{
如果(rbUK.Checked)
{
//阅读一次通话中的所有线路,
string[]lines=File.ReadAllLines(@“.\Debug\PostageCosts.csv”);
//我希望有三行,但最好在对阵列执行操作之前检查
如果(lines.Length>0)
{
//为UK拆分行,仅使用前6个值
//对于列表(不应添加最后的字符串“每千克英国”)
//同样,至少6个十进制值应不少于。。。
变量值=行[0]。拆分(',');
如果(values.Length>=6)
对于(int x=0;x<6;x++)
Add(decimal.Parse(值[x]);
}
如果(lines.Length>1)
{
变量值=行[1]。拆分(',');
如果(values.Length>=6)
对于(int x=0;x<6;x++)
Add(decimal.Parse(值[x]);
}
如果(线条长度>2)
{
变量值=行[2]。拆分(',');
如果(values.Length>=6)
对于(int x=0;x<6;x++)
Add(decimal.Parse(值[x]);
}
//通过索引0,1,2测试要显示的列表
int listToShow=0;
显示(列表显示);
}
}
公共空白显示(int listToShow)
{
列表显示列表=(列表显示==0?英国:
listToShow==1?剩余欧洲:
全球);
label2.Text=string.Join(“-”,showList.ToArray());
}

在文件中,元素之间是否有空格或制表符?您将字符串拆分为“,”但行中没有逗号。这是为大学准备的,我认为它使用了类似于“is out”的内容?出现了什么错误?@VolkanPaksoy,这就是为什么我问分隔符是什么。我已经试过了,除了我想输出值的时候,一切都很好。“label2.Text=UK[curValue];”结尾应该是.ToString()吗?是的,添加它,您应该会看到您的十进制值使用文化的格式转换转换转换为字符串。我尝试过,但它说您无法从十进制转换为int?您能说明您正在尝试做什么吗?(引发错误的行)
List<decimal> UK = new List<decimal>();
List<decimal> RestOfEurope = new List<decimal>();
List<decimal> Worldwide = new List<decimal>();
  decimal curValue;
  if(decimal.TryParse(values[0], out curValue))
     UK.Add(curValue);
  //else
  //   ... if you want some kind of error reporting here ...
private void destination()
{

    if (rbUK.Checked)
    {
        // Read all the lines in a single call, 
        string[] lines = File.ReadAllLines(@".\Debug\PostageCosts.csv");

        // I expect to have three lines but better to check before acting on the array

        if(lines.Length > 0)
        {
            // Split the line for UK and use only the first 6 values
            // for the list (the last strings 'per kg  UK' should not be added)
            // Again, at least 6 decimal values are expected not less...
            var values = lines[0].Split(',');
            if(values.Length >= 6)
                for(int x = 0; x < 6; x++)
                    UK.Add(decimal.Parse(values[x]));
        }
        if(lines.Length > 1)
        {
            var values = lines[1].Split(',');
            if(values.Length >= 6)
                for(int x = 0; x < 6; x++)
                    RestOfEurop.Add(decimal.Parse(values[x]));
        }
        if(lines.Length > 2)
        {
            var values = lines[2].Split(',');
            if(values.Length >= 6)
                for(int x = 0; x < 6; x++)
                    WorlWide.Add(decimal.Parse(values[x]));
        }
        // Test what list to display passing the index 0,1,2
        int listToShow = 0;
        display(listToShow);
    }
}
public void display(int listToShow)
{
    List<decimal> showList = (listToShow == 0 ? UK : 
                              listToShow == 1 ? RestOfEurope : 
                                                WorlWide);
    label2.Text = string.Join("-",  showList.ToArray());
}