如何在加载函数中将csv文件数据导入DataGridView [英] How to import csv file data into DataGridView in load function

查看:123
本文介绍了如何在加载函数中将csv文件数据导入DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我想将 csv 文件数据导入我的 datagridview 到我的加载函数中,



我不知道如何为它编写代码。



这里是我的代码:



In my programme i want to import my csv file data into my datagridview into my load function,

I have no idea how to write the code for it.

here is my code:

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 VMwareRemoteConsoleTypeLib;

namespace OVF_ImportExport
{
    public partial class Form1 : Form
    {
        string sPath="";
        string sName = "";
        string delimiter = ",";
        string tablename = "export";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

           string filename= ("c:\\izaz\\test.csv");
            DataSet dataset = new DataSet();
            StreamReader sr = new StreamReader(filename);
            string allData = sr.ReadToEnd();
            string[] rows = allData.Split("\r".ToCharArray());
            foreach (string r in rows)
            {
                string[] items = r.Split(delimiter.ToCharArray());
                dataset.Tables[tablename].Rows.Add(items);
            }
            this.dataGridView1.DataSource = dataset.Tables[0].DefaultView;

                 
            
            //System.Data.OleDb.OleDbConnection MyConnection;
            //System.Data.DataSet DtSet;
            //System.Data.OleDb.OleDbDataAdapter MyCommand;
            //MyConnection = new System.Data.OleDb.OleDbConnection(@"provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\psave\New folder\result.xlsx';Extended Properties=Excel 8.0;");
           // MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
           // MyCommand.TableMappings.Add("Name", "Path");
            //DtSet = new System.Data.DataSet();
            //MyCommand.Fill(DtSet);
            //dataGridView1.DataSource = DtSet.Tables[0];
            //MyConnection.Close();
            //dataGridView1.Columns[0].Width = 300;
            //dataGridView1.Columns[1].Width = 450;
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                sName = row.Cells[0].Value.ToString();
                sPath = row.Cells[1].Value.ToString();

                           
            }
        }

        private void BtnBrowse_Click(object sender, EventArgs e)
        {
            fbd.ShowDialog();
            TxtBrowsepath.Text = fbd.SelectedPath;
        }

        private void BtnCreate_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
            StreamWriter file = new StreamWriter("Export.bat");
            file.WriteLine("c: ");
            file.WriteLine("cd \\");
            file.WriteLine("cd Program Files " );
            file.WriteLine("cd VMware" );
            file.WriteLine("cd VMware OVF Tool" );

            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                sName = row.Cells[0].Value.ToString();
                sPath = row.Cells[1].Value.ToString();

                //richTextBox1.Text = richTextBox1.Text + Environment.NewLine + sName;
                file.WriteLine("start ovftool.exe --powerOffSource vi://"+TxtUsername.Text + ":" + TxtPassword.Text + "@"+ TxtIP.Text + sPath + " " + "\"" + TxtBrowsepath.Text + "\\" + sName + "\\"+ sName + ".ovf" + "\"" + Environment.NewLine);
                   
                
            }
            file.WriteLine("pause");
            file.Close();
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        }
        
        }

推荐答案

,MyConnection );
// MyCommand.TableMappings.Add(Name,Path) ;
// DtSet = new System.Data.DataSet();
// MyCommand.Fill(DtSet);
// dataGridView1.DataSource = DtSet.Tables [0];
// MyConnection.Close();
// dataGridView1.Columns [0] .Width = 300;
// dataGridView1.Columns [1] .Width = 45 0;
}

private void dataGridView1_CellMouseClick ( object sender,DataGridViewCellMouseEventArgs e)
{

foreach ( DataGridViewRow行 in dataGridView1.SelectedRows)
{
sName = row.Cells [ 0 ] Value.ToString();
sPath = row.Cells [ 1 ]。Value.ToString();


}
}

private void BtnBrowse_Click( object sender,EventArgs e)
{
fbd.ShowDialog();
TxtBrowsepath.Text = fbd.SelectedPath;
}

private void BtnCreate_Click( object sender,EventArgs e)
{
richTextBox1.Text = ;
StreamWriter file = new StreamWriter( Export .BAT);
file.WriteLine( c:);
file.WriteLine( cd \\);
file.WriteLine( cd Program Files);
file.WriteLine( cd VMware);
file.WriteLine( cd VMware OVF Tool);

foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
sName = row.Cells [ 0 ]。Value.ToString();
sPath = row.Cells [ 1 ]。Value.ToString();

// richTextBox1.Text = richTextBox1.Text + Environment.NewLine + sName;
file.WriteLine( start ovftool.exe --powerOffSource vi:// + TxtUsername.Text + + TxtPassword.Text + @ + TxtIP.Text + sPath + + \ + TxtBrowsepath.Text + < span class =code-string>
\\ + sName + \\ + sName + 。ovf + \ + Environment.NewLine);


}
file.WriteLine( pause);
file.Close();
}

private void BtnClose_Click( object sender,EventArgs e)
{
this .Close();
}

}

}
", MyConnection); // MyCommand.TableMappings.Add("Name", "Path"); //DtSet = new System.Data.DataSet(); //MyCommand.Fill(DtSet); //dataGridView1.DataSource = DtSet.Tables[0]; //MyConnection.Close(); //dataGridView1.Columns[0].Width = 300; //dataGridView1.Columns[1].Width = 450; } private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) { foreach (DataGridViewRow row in dataGridView1.SelectedRows) { sName = row.Cells[0].Value.ToString(); sPath = row.Cells[1].Value.ToString(); } } private void BtnBrowse_Click(object sender, EventArgs e) { fbd.ShowDialog(); TxtBrowsepath.Text = fbd.SelectedPath; } private void BtnCreate_Click(object sender, EventArgs e) { richTextBox1.Text = ""; StreamWriter file = new StreamWriter("Export.bat"); file.WriteLine("c: "); file.WriteLine("cd \\"); file.WriteLine("cd Program Files " ); file.WriteLine("cd VMware" ); file.WriteLine("cd VMware OVF Tool" ); foreach (DataGridViewRow row in dataGridView1.SelectedRows) { sName = row.Cells[0].Value.ToString(); sPath = row.Cells[1].Value.ToString(); //richTextBox1.Text = richTextBox1.Text + Environment.NewLine + sName; file.WriteLine("start ovftool.exe --powerOffSource vi://"+TxtUsername.Text + ":" + TxtPassword.Text + "@"+ TxtIP.Text + sPath + " " + "\"" + TxtBrowsepath.Text + "\\" + sName + "\\"+ sName + ".ovf" + "\"" + Environment.NewLine); } file.WriteLine("pause"); file.Close(); } private void BtnClose_Click(object sender, EventArgs e) { this.Close(); } } }


我使用过这样的解决方案,我希望这对您有用。

将文本和CSV文件中的数据导入DataGridView

第1步:首先我将代码放在按钮点击事件中,这将在您的计算机中浏览.csv,您可以更改为加载功能。

I have used solution like this, i hope this will work for you.
Import Data from Text and CSV file to DataGridView
STEP 1 : First i put the code in Button click event this will Browse your .csv in your computer, you can change to load function.
OpenFileDialog fdlg = new OpenFileDialog();
    fdlg.Title = "Select file";
    fdlg.InitialDirectory = @"c:\";
    fdlg.FileName = txtFileName.Text;
    fdlg.Filter = "Text and CSV Files(*.txt, *.csv)|*.txt;*.csv|Text Files(*.txt)|*.txt|CSV Files(*.csv)|*.csv|All Files(*.*)|*.*";
    fdlg.FilterIndex = 1;
    fdlg.RestoreDirectory = true;
    if (fdlg.ShowDialog() == DialogResult.OK)
    {
        txtFileName.Text = fdlg.FileName;
        Import();
        Application.DoEvents();
    }





第2步:创建导入功能(),用于第1步



STEP 2 : Create Import function(), this used in STEP 1

if (txtFileName.Text.Trim() != string.Empty)
{
    try
    {
        DataTable dt = GetDataTable(txtFileName.Text);
        dataGridView1.DataSource = dt.DefaultView;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }
}





第3步:创建像从数据集中的文件数据返回值。



STEP 3 : Create something like return value from file data in a dataset.

public static DataTable GetDataTable(string strFileName)
{
    ADODB.Connection oConn = new ADODB.Connection();
    oConn.Open("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\";", "", "", 0);
    string strQuery = "SELECT * FROM [" + System.IO.Path.GetFileName(strFileName) + "]";
    ADODB.Recordset rs = new ADODB.Recordset();
    System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter();
    DataTable dt = new DataTable();
    rs.Open(strQuery, "Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\";",
        ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, 1);
    adapter.Fill(dt, rs);
    return dt;
}





注意:如果您有任何错误请尝试将目标平台设置为X86 您的申请


这篇关于如何在加载函数中将csv文件数据导入DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆