Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 4th, 2009, 04:33 AM
Registered User
 
Join Date: Mar 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default append data in datagridview

I have already had some data in datagrid view and i want to import fresh records from excel file in such a way that imported data shd append in datagridview.


The code I provided is only get data from excel file and import to dgv. What modification shd i made that it would append and keep previous data in datagridview.







Code:
private DataTable getDataFromXLS(string strFilePath)
        {
            try
            {
                string strConnectionString = "";  //Data Source=HARIS\SQLEXPRESS;Initial Catalog=MFSAS;User ID=akram
                strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strFilePath + "; Jet OLEDB:Engine Type=5;" + "Extended Properties=Excel 8.0;";
                OleDbConnection conn = new OleDbConnection(strConnectionString);
                conn.Open();
                OleDbCommand cmd = conn.CreateCommand();
                cmd.CommandText = "Select * FROM [Sheet1$]";
                OleDbDataAdapter da = new OleDbDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                conn.Close();
                da = null;
                return dt;
            }

            catch (Exception ex)
            {
                return null;
            }
            finally { }
        }
        private void btnImportData_Click(object sender, EventArgs e)
        {
            DataTable test = getDataFromXLS(@"c:\temp.xls");
            if (test != null)
            {
                dgvViewJDRecords.DataSource = test;
            }
        }
 
Old March 4th, 2009, 06:38 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Because your grid is bounded to the source, you have to append the new data to the datatable you are supplying to the datagrid.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old March 4th, 2009, 06:45 AM
Registered User
 
Join Date: Mar 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default dgv issue

dgvViewJDRecords.DataSource = test;


which property shd i use to append data instead of <DataSource>
 
Old March 4th, 2009, 06:52 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

DataSource is the correct one.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
append/insert data to xml file andhiez XML 5 April 18th, 2008 11:13 AM
create table/append data. bpdineen Access VBA 1 January 4th, 2007 11:47 AM
What is the best method to append data? rdmapes PHP How-To 0 December 27th, 2006 01:06 PM
Save data on datagridview salemkoten SQL Server 2005 1 November 2nd, 2006 11:04 PM
append data to xml andhiez General .NET 0 June 14th, 2005 04:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.