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 :-

Code:
        // 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. I'll be using a cmd button to launch the copy.
eg:- DataTable dtSelectedColumns = dtRaw.DefaultView.ToTable(false, dgcolumn1, dgcolumn2, dgcolumn3, dgcolumn4);

Any suggestions would be great.

Thanks.