[RESOLVED] Copy from datatable to new datatable
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: [RESOLVED] Copy from datatable to new datatable

  1. #1
    Join Date
    Aug 2012
    Posts
    5

    [RESOLVED] Copy from datatable to new datatable

    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.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,097

    Re: [RESOLVED] Copy from datatable to new datatable

    I see you have marked your thread resolved. Mind sharing your answer??

  3. #3
    Join Date
    Aug 2012
    Posts
    5

    Re: [RESOLVED] Copy from datatable to new datatable

    I had part of the code in the incorrect scope, so it was localised to a command button & not being shareable.

    I then used this code to copy the selected columns to the second datatable :-
    DataTable2= DataTable1.DefaultView.ToTable(false, column1, column2, column3, column4);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center