OpenFileDialogue控件将文本框转换为DataTable [英] OpenFileDialogue control to Textbox to DataTable

查看:103
本文介绍了OpenFileDialogue控件将文本框转换为DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在表单上有一个DataGridView,一个textBox和两个按钮.

I have a DataGridView, a textBox and two buttons on a form.

我希望第一个按钮浏览"打开一个OpenFileDialogue,以选择一个文件.

I want the first button, Browse, to open up a OpenFileDialogue, to choose a file.

然后我希望将该值添加到文本框中.

Then I want that value to be added to the Textbox.

最后,当第二个按钮Submit按下时,我希望使用TextBox中的值来帮助填充DGV.

Finally, I want the value in the TextBox to be used to help populate the DGV when the second button, Submit, is pressed.

到目前为止,这是我的代码:

Here is my code so far:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;using System.Data.OleDb;namespace browseWindow{    public partial class Form1 : Form    {            public Form1()        {            InitializeComponent();        }        private void button_Browse_Click(object sender, EventArgs e)        {            openFD.InitialDirectory = @"E:\aBULKProjects\aaaTAXI_RANK\_Unconstrained Funds\XML_Export";            openFD.Filter = "xls|*.xls|All|*.*";            openFD.ShowDialog();        }        private DataTable dt;        private void button_Submit_Click(object sender, EventArgs e)        {            this.dt = getDataFromXLS("E:\\zzzPERSONAL\\zzLee\\risk.xls");            if (this.dt != null)                dgv.DataSource = this.dt;        }        private DataTable getDataFromXLS(string strFilePath)        {            try            {                string strConnectionString = "";                strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +                                                 "Data Source=" + strFilePath + "; Jet OLEDB:Engine Type=5;" +                                                 "Extended Properties=Excel 8.0;";                OleDbConnection cnCSV = new OleDbConnection(strConnectionString);                cnCSV.Open();                OleDbCommand cmdSelect = new OleDbCommand(@"SELECT * FROM [use$]", cnCSV);                OleDbDataAdapter daCSV = new OleDbDataAdapter(); daCSV.SelectCommand = cmdSelect;                DataTable dtCSV = new DataTable();                daCSV.Fill(dtCSV);                cnCSV.Close();                daCSV = null;                return dtCSV;            }            catch (Exception ex)            {                throw (ex);            }            finally            {            }        }    }}

到目前为止,所有这些操作都是在OpenFileDialogue中设置初始目录的路径,并在Submit按钮中放置一个路径,该按钮会将XLS数据发送到DGV.

All this does so far is sets the path for the initial directory in the OpenFileDialogue and puts a path in the Submit button, which sends the XLS data to the DGV.

我假定必须创建一个为其选择文件的变量,然后必须用该变量替换固定路径,然后将其用于填充TextBox.最后,该变量在OleDBCommand中用于Select语句.

I assume that a variable must be created for which file is choosen, then the fixed paths must be replaced with that variable, which then is used to populate the TextBox. Finally the variable is used in the OleDBCommand for the Select statement.

但是,如果这样做是正确的方法,则不确定如何执行此操作!

Not sure however, how to do this, if this is the right way to do this!

任何建议都值得赞赏.

李·沃伦

推荐答案

这是我在网上找到的可以做到的文章.我本可以写点东西,但是重新发明轮子没有意义.以下是带有示例源代码的链接:
Here is an article I found online that can do it. I could have written something up, but no sense in reinventing the wheel. Here is the link with sample source code: http://www.c-sharpcorner.com/uploadfile/ankurmee/import-data-from-excel-to-datagridview-in-C-Sharp/


这篇关于OpenFileDialogue控件将文本框转换为DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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