Question Copy from datatable to new datatable

charlie20

Member
Joined
Aug 27, 2012
Messages
11
Programming Experience
Beginner
Hi all,

Hoping for some pointers / advice.........

I have a winforms application that imports data from a flat file into a datatable.

I need to select certain columns from this import & use there to populate a second datatable, as well as a dataview grid.

The following is the code that I am using to import the data :-

C#:
        // Select & open file
        private void button1_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Select a File to import";
            fdlg.InitialDirectory = @"c:\";
            fdlg.FileName = txtFileName.Text;
            fdlg.Filter = "Comma Seperated Values Files|*.csv";
            fdlg.FilterIndex = 1;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                txtFileName.Text = fdlg.FileName;
                ImportRaw();
                Application.DoEvents();
            }
        }
        // Import file into DataTable 
        private void ImportRaw()
        {
            if (txtFileName.Text.Trim() != string.Empty)
            {
                try
                {
                    // Populate the DataTable
                    DataTable dtRaw = GetDataTableRaw(txtFileName.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        public static DataTable GetDataTableRaw(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 dtRaw = 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(dtRaw, rs);
            return dtRaw;
            
        }
I've tried various things to select the columns I want into a new datatable & linked to a datagridview - all with no success unfortunately.
eg:- DataTable dtSelectedColumns = dtRaw.DefaultView.ToTable(false, dgcolumn1, dgcolumn2, dgcolumn3, dgcolumn4);

Any suggestions would be great.

Thanks.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,150
Location
Sydney, Australia
Programming Experience
10+
That DataView.ToTable method requires the names of the columns in the DataTable that you want to copy. This has nothing whatsoever to do with the grid. Is 'dgcolumn1' a string variable containing the name of a column in 'dtRaw'? If not then of course it's not going to work.
 

charlie20

Member
Joined
Aug 27, 2012
Messages
11
Programming Experience
Beginner
Thanks for the reply - dgcolumn1 is indeed a string variable.
Turned out to be a simple error - I had part of the code relating to dtRaw in the incorrect scope (sigh).
 
Top Bottom