提问者:小点点

将逗号分隔的列表写入数据网格视图


我试图将一个csv文件放入数据表中,但是csv文件中有一些东西我试图省略,不要输入到数据表中,所以我先将它写到一个列表中。 我将使用该软件的csv文件中有不同的部分,我然后将整个列表拆分成这些部分的单独列表。 在完成所有这些之后,我需要跳过每个列表中的一些行,然后写出我满意的最终表单,分别列出之前的一组列表。

现在我碰壁了,我需要将每个列表写入各自的数据网格。

   public partial class Form1 : Form
{

    String filePath = "";

    //list set 1 = list box
    List<String> lines = new List<String>();
    List<String> accountList = new List<String>();
    List<String> statementList = new List<String>();
    List<String> summaryList = new List<String>();
    List<String> transactionList = new List<String>();

    //list set 2 = dgv
    List<String> accountList2 = new List<String>();
    List<String> statementList2 = new List<String>();
    List<String> summaryList2 = new List<String>();
    List<String> transactionList2 = new List<String>();

    public Form1()
    {
        InitializeComponent();
    }

    private void btn_find_Click(object sender, EventArgs e)
    {
        try
        {
           
            using (OpenFileDialog fileDialog = new OpenFileDialog()
            { Filter = "CSV|* .csv", ValidateNames = true, Multiselect = false })
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    String fileName = fileDialog.FileName;
                
                    filePath = fileName;
                }
            
            try
            {
                if (File.Exists(filePath))
                {
                    lines = File.ReadAllLines(filePath).ToList();

                    foreach (String line in lines)
                    {

                        String addLine = line.Replace("'", "");
                        String addLine2 = addLine.Replace("\"", "");
                        String str = line.Substring(0, 1);
                        int num = int.Parse(str);

                        if (addLine2.Length > 1)
                        {
                            String addLine3 = addLine2.Substring(2);

                            switch (num)
                            {
                                case 2:
                                    accountList.Add(addLine3);
                                    break;

                                case 3:
                                    statementList.Add(addLine3);
                                    break;

                                case 4:
                                    summaryList.Add(addLine3);
                                    break;

                                case 5:
                                    transactionList.Add(addLine3);
                                    break;

                            }

                        }

                    }

                }
                else
                {
                    MessageBox.Show("Invalid file chosen, choose an appropriate CSV file and try again.");
                }

                transactionLB.DataSource = transactionList;
                //var liness = transactionList;
                //foreach (string line in liness.Skip(2))
                //    transactionList2.Add(line);
                //Console.WriteLine(transactionList2);

                //var source = new BindingSource();
                //source.DataSource = transactionList2;
                //trans_dgv.DataSource = source; 


                accountLB.DataSource = accountList;
                
                
                summaryLB.DataSource = summaryList;
                
                
                statementLB.DataSource = statementList;

            }
            catch (Exception)
            {
                MessageBox.Show("Cannot load CSV file, Ensure that a valid CSV file is selected and try again.");
            }
        }
        catch (Exception)
        {
            MessageBox.Show("Cannot open File Explorer, Something is wrong :(");
        }
    }



}

共1个答案

匿名用户

如果我没有理解错的话,您似乎想让字符串列表中的字符串的值出现在DataGridViews中。 这可能有点棘手,因为DataGridView需要知道要显示哪个属性,而字符串只有Length属性(这可能不是您要查找的)。 有很多方法可以将所需的数据获取到DataGridView中。 例如,您可以使用DataTables并选择要在DataGridView中显示的列。 如果您想坚持使用字符串列表,我认为您可以通过修改DataSource行使其看起来像这样来实现这一点:

transactionLB.DataSource = transactionList.Select(x => new { Value = x} ).ToList();

我希望这能帮上忙! 如果我误解了你的问题,请告诉我。 谢啦!