微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C# 使用 2 个列表框从 CSV 中选择所需的列并在 dataGridView 中显示

如何解决C# 使用 2 个列表框从 CSV 中选择所需的列并在 dataGridView 中显示

我正在开发的程序有 2 个列表框、2 个 dataGridView、用于在 2 个列表框之间移动所选项目的按钮,以及一个带有用于选择和加载 CSV 的按钮的文本框。选择 CSV 后,listBox1 将填充来自 CSV 的所有标题。单击“导入原始结果”后,数据将显示在 dataGridView1 中。您将如何使用 listBox2 选择要在 dataGridView2 中显示标题(列)并显示 CSV 中的所有选定数据?

我需要构建的方法从第 135 行开始- selectedResultsCsv()

View of running program

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;
using Microsoft.VisualBasic.FileIO; // add reference to Microsoft.VisualBasic for CSV parsing

namespace ImportTimeCardCSV
{
    public partial class ResultsForm : Form
    {
        public ResultsForm()
        {
            InitializeComponent();
        }//End ResultsForm
        private void ResultsForm_Load(object sender,EventArgs e)
        {
        
        }//End ResultsForm_Load

        DataTable ResultsTable = new DataTable();
        private DataTable selectedResultsTable = new DataTable();           //has not been used yet

                   //
                   //Below Region has all code for Open File Dialog - opening Results CSV
                   //
        #region OpenFileDialog
        private void resultsCsvOpenFileDialog()
        {
            OpenFileDialog ResultsOpenFileDialog = new OpenFileDialog();

            ResultsOpenFileDialog.Title = "Choose a Results CSV File";
            ResultsOpenFileDialog.DefaultExt = "csv";
            ResultsOpenFileDialog.Filter = "csv files (*.csv)|*.csv|txt files (*.txt)|*.txt|All files         (*.*)|*.*";
            ResultsOpenFileDialog.FilterIndex = 1;
            ResultsOpenFileDialog.RestoreDirectory = true;

            if (ResultsOpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtResultsCsvFilename.Text = ResultsOpenFileDialog.FileName;
                listBox1.Items.Clear();
            
                using (textfieldparser parser = new textfieldparser(txtResultsCsvFilename.Text))        //Read the headers and add to listBox1
                {
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters(";");
                    string[] fields = parser.ReadFields();

                    int Index = 0;
                    foreach (string field in fields)
                    {
                        if (chkResultsCsvHeaders.Checked)
                        {
                            //listBox1.Items.Add(Index.ToString() + ":" + field);                   //displays the number of the column and header name,ex: 1: Name 
                            listBox1.Items.Add(field);                                              //displays only the column header name,ex: Name
                        }
                        else
                        {
                            listBox1.Items.Add("Column " + Index.ToString() + ": " + field);        //displays Column,the number of the column,and header name,ex: Column 1: Name 
                        }
                        Index++;
                    }
                    parser.Close();
                }//end using
            }//end if
        }//End resultsCsvOpenFileDialog()
        private void cmdResultsCsvOpenFileDialog_Click(object sender,EventArgs e)
        {
            resultsCsvOpenFileDialog();
        }//End cmdResultsCsvOpenFileDialog_Click
        #endregion
                    //
                    //Below Region has all code for Importing Results - From CSV to Datagridviews
                    //
        #region Import
        private void importToGridButton_Click(object sender,EventArgs e)
        {
            importResultsCsv();
        }//End importToGridButton_Click
        private void importResultsCsv()
        {
            if (txtResultsCsvFilename.Text == "")
            {
                MessageBox.Show("Select a Results CSV to load.");
            }
            else
            {
                string[] results_text = File.ReadAllLines(txtResultsCsvFilename.Text);
                string[] data_col = null;
                int x = 0;
                int RowIndex = 0;
                foreach (string text_line in results_text)
                {
                    data_col = text_line.Split(';');
                    if (x == 0)
                    {
                        //header
                        for (int i = 0; i <= data_col.Count() - 1; i++)
                        {
                            ResultsTable.Columns.Add(data_col[i]);
                        }
                        x++;
                    }
                    else
                    {
                        //data
                        ResultsTable.Rows.Add(data_col);
                    }
                    RowIndex++;
                    lblResultsProcessed.Text = RowIndex + " Result(s)";
                }//end foreach
                dataGridView1.DataSource = ResultsTable;
            }//end else
        }//End importResultsCsv
        private void cmdselectedResultImport_Click(object sender,EventArgs e)
        {   
            DialogResult dialog = MessageBox.Show("Please confirm the selections you have made.","Confirm Selections",MessageBoxButtons.YesNo,MessageBoxIcon.Question);

            if (dialog == DialogResult.Yes)
            {
                //dataGridView2.DataSource = selectedResultsTable;        //NEED SELECTED RESULTS
                selectedResultsCsv();
            }
            else
            {
                MessageBox.Show("Please select at least one result.","Selection",MessageBoxButtons.OK,MessageBoxIcon.information);
            }
        }//End cmdselectedResultImport_Click

        private void selectedResultsCsv()
        {

            //method in questioning 

        }//End selectedResultsCsv
        #endregion

                    ///
                    ///Below Region has all code for the buttons working with the list Boxes
                    ///
        #region Buttons for listBoxes
        private void rightButton_Click(object sender,EventArgs e)
        {
            if (listBox1.Selectedindex == -1)
            {
                MessageBox.Show("Please select an item.");
            }
            else
            {
                listBox2.Items.Add(listBox1.SelectedItem);
                int i = 0;
                i = listBox1.Selectedindex;
                //listBox1.Items.RemoveAt(i);
            }
        }//End rightButton_Click
        private void multipleRightButton_Click(object sender,EventArgs e)
        {
            if (listBox1.Selectedindex == -1)
            {
                MessageBox.Show("Please select an item.");
            }
            else
            {
                while (listBox1.SelectedItems.Count != 0)
                {
                    foreach (var items in listBox1.SelectedItems)
                    {
                        listBox2.Items.Add(items);
                    }

                    for (int i = listBox1.SelectedItems.Count - 1; i >= 0; i--)
                    {
                        listBox1.Items.Remove(listBox1.SelectedItems[i]);
                    }
                }
            }
        }//End multipleRightButton_Click
        private void moveAllRightButton_Click(object sender,EventArgs e)
        {
            while (listBox1.Items.Count != 0)
            {
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    listBox2.Items.Add(listBox1.Items[i]);
                    listBox1.Items.Remove(listBox1.Items[i]);
                }
            }
        }//End moveAllRightButton_Click
        private void leftButton_Click(object sender,EventArgs e)
        {
            if (listBox2.Selectedindex == -1)
            {
                MessageBox.Show("Please select an item.");
            }
            else
            {
                listBox1.Items.Add(listBox2.SelectedItem);
                int i = 0;
                i = listBox2.Selectedindex;
                listBox2.Items.RemoveAt(i);
            }
        }//End leftButton_Click
        private void multipleLeftButton_Click(object sender,EventArgs e)
        {
            if (listBox2.Selectedindex == -1)
            {
                MessageBox.Show("Please select an item.");
            }
            else
            {
                while (listBox2.SelectedItems.Count != 0)
                {
                    foreach (var items in listBox2.SelectedItems)
                    {
                        listBox1.Items.Add(items);
                    }

                    for (int i = listBox2.SelectedItems.Count - 1; i >= 0; i--)
                    {
                        listBox2.Items.Remove(listBox2.SelectedItems[i]);
                    }
                }
            }
        }//End multipleLeftButton_Click
        private void moveAllLeftButton_Click(object sender,EventArgs e)
        {
            while (listBox2.Items.Count != 0)
            {
                for (int i = 0; i < listBox2.Items.Count; i++)
                {
                    listBox1.Items.Add(listBox2.Items[i]);
                    listBox2.Items.Remove(listBox2.Items[i]);
                }
            }
        }//End moveAllLeftButton_Click
        private void upButton_Click(object sender,EventArgs e)
        {
            if (listBox2.Selectedindex == -1)
            {
                MessageBox.Show("Please select an item.");
            }
            else
            {
                int i = listBox2.Selectedindex;

                string item = listBox2.SelectedItem.ToString();
                if (i > 0)
                {
                    listBox2.Items.RemoveAt(i);
                    listBox2.Items.Insert(i - 1,item);
                    listBox2.SetSelected(i - 1,true);
                }
            }
        }//End upButton_Click
        private void downButton_Click(object sender,EventArgs e)
        {
            if (listBox2.Selectedindex == -1)
            {
                MessageBox.Show("Please select an item.");
            }
            else
            {
                int i = listBox2.Selectedindex;

                string item = listBox2.SelectedItem.ToString();
                if (i < listBox2.Items.Count - 1)
                {
                    listBox2.Items.RemoveAt(i);
                    listBox2.Items.Insert(i + 1,item);
                    listBox2.SetSelected(i + 1,true);
                }
            }
        }//End downButton_Click
        private void deleteButton_Click(object sender,EventArgs e)
        {
            if (listBox2.Selectedindex == -1)
            {
                MessageBox.Show("Please select an item.");
            }
            else
            {
                int i = 0;
                i = listBox2.Selectedindex;
                listBox2.Items.RemoveAt(i);
            }
        }//End deleteButton_Click
        private void clearButton_Click_1(object sender,EventArgs e)
        {
            while (listBox2.Items.Count != 0)
            {
                if (listBox2.Items.Count != 0)
                {
                    MessageBox.Show("Select a Results CSV.");
                }

                listBox2.Items.Clear();
                listBox1.Items.Clear();
                resultsCsvOpenFileDialog();
            }
        }//End clearButton_Click_1
        #endregion

    }//End ResultsForm : Form

}//End ImportTimeCardCSV

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

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?